-
Notifications
You must be signed in to change notification settings - Fork 1
feat(storage): updateBucket method #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bbc22bc
feat(storage): updateBucket method
designcode 1fbe814
fix(storage): error logic when no organization id is provided
designcode 960c04d
fix(storage): error logic when no organization id is provided
designcode 5d37455
feat(storage): add more options to updateBucket
designcode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import { TigrisHeaders } from '@shared/headers'; | ||
| import { createStorageClient } from '../http-client'; | ||
| import type { TigrisStorageConfig, TigrisStorageResponse } from '../types'; | ||
|
|
||
| export type UpdateBucketOptions = { | ||
| // access and sharing settings | ||
| access?: 'public' | 'private'; | ||
| allowObjectAcl?: boolean; | ||
| disableDirectoryListing?: boolean; | ||
|
|
||
| // storage settings | ||
| // consistency?: 'strict' | 'default'; | ||
| regions?: string | string[]; | ||
| cacheControl?: string; | ||
|
|
||
| // data management settings | ||
| // TODO: Data Migration, TTL Config, Objects Lifecycle | ||
|
|
||
| // custom domain | ||
| customDomain?: string; | ||
|
|
||
| // cors settings | ||
| // TODO: Additional Headers, CORS Rules | ||
|
|
||
| // notification settings | ||
| // TODO: enableNotifications?: boolean; | ||
|
|
||
| // deletion settings | ||
| enableDeleteProtection?: boolean; | ||
|
|
||
| config?: Omit<TigrisStorageConfig, 'bucket'>; | ||
| }; | ||
|
|
||
| export type UpdateBucketResponse = { | ||
| bucket: string; | ||
| updated: boolean; | ||
| }; | ||
|
|
||
| export async function updateBucket( | ||
| bucketName: string, | ||
| options?: UpdateBucketOptions | ||
| ): Promise<TigrisStorageResponse<UpdateBucketResponse, Error>> { | ||
| const { data: client, error } = createStorageClient(options?.config); | ||
|
|
||
| if (error || !client) { | ||
| return { error }; | ||
| } | ||
|
|
||
| const body: Record<string, unknown> = {}; | ||
| const headers: Record<string, string> = {}; | ||
|
|
||
| // access and sharing settings | ||
| if (options?.access !== undefined) { | ||
| headers[TigrisHeaders.ACL] = | ||
| options.access === 'public' ? 'public-read' : 'private'; | ||
| } | ||
|
|
||
| if (options?.allowObjectAcl !== undefined) { | ||
| body.acl_settings = { allow_object_acl: options.allowObjectAcl }; | ||
| } | ||
|
|
||
| if (options?.disableDirectoryListing !== undefined) { | ||
| headers[TigrisHeaders.ACL_LIST_OBJECTS] = | ||
| options.disableDirectoryListing === true ? 'false' : 'true'; | ||
| } | ||
|
|
||
| // storage settings | ||
| /*if (options?.consistency !== undefined) { | ||
| body.consistent = options.consistency === 'strict' ? true : false; | ||
| }*/ | ||
|
|
||
| if (options?.regions !== undefined) { | ||
| body.object_regions = Array.isArray(options.regions) | ||
| ? options.regions.join(',') | ||
| : options.regions; | ||
| } | ||
|
|
||
| if (options?.cacheControl !== undefined) { | ||
| body.cache_control = options.cacheControl; | ||
| } | ||
|
|
||
| // custom domain | ||
| if (options?.customDomain !== undefined) { | ||
| body.website = { domain_name: options.customDomain }; | ||
| } | ||
|
|
||
| // deletion settings | ||
| if (options?.enableDeleteProtection !== undefined) { | ||
| body.protection = { protected: options.enableDeleteProtection }; | ||
| } | ||
|
|
||
| const response = await client.request< | ||
| Record<string, unknown>, | ||
| { status: 'success' | 'error'; message?: string } | ||
| >({ | ||
| method: 'PATCH', | ||
| path: `/${bucketName}`, | ||
| body, | ||
| headers, | ||
| }); | ||
|
|
||
| if (response.error) { | ||
| return { error: response.error }; | ||
| } | ||
|
|
||
| if (response.data.status === 'error') { | ||
| return { | ||
| error: new Error(response.data.message ?? 'Failed to update bucket'), | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| data: { | ||
| bucket: bucketName, | ||
| updated: true, | ||
| }, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { createTigrisHttpClient, type TigrisHttpClient } from '@shared/index'; | ||
| import { config } from './config'; | ||
| import type { TigrisStorageConfig, TigrisStorageResponse } from './types'; | ||
|
|
||
| function getStorageEndpoint(options?: TigrisStorageConfig): string { | ||
| return options?.endpoint ?? config.endpoint ?? 'https://t3.storage.dev'; | ||
| } | ||
|
|
||
| export function createStorageClient( | ||
| options?: TigrisStorageConfig | ||
| ): TigrisStorageResponse<TigrisHttpClient, Error> { | ||
| const sessionToken = options?.sessionToken ?? config.sessionToken; | ||
| const organizationId = options?.organizationId ?? config.organizationId; | ||
| const accessKeyId = options?.accessKeyId ?? config.accessKeyId; | ||
| const secretAccessKey = options?.secretAccessKey ?? config.secretAccessKey; | ||
|
|
||
| // Allow either session token, authorization, or credentials | ||
| const hasCredentials = accessKeyId && secretAccessKey; | ||
| if (!sessionToken && !hasCredentials) { | ||
| return { | ||
| error: new Error('Session token or credentials are required'), | ||
| }; | ||
| } | ||
|
|
||
| if (sessionToken && (!organizationId || organizationId === '')) { | ||
| return { error: new Error('Organization ID is required') }; | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return createTigrisHttpClient({ | ||
| baseUrl: getStorageEndpoint(options), | ||
| sessionToken, | ||
| organizationId, | ||
| accessKeyId, | ||
| secretAccessKey, | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are some other options as well like object notification, object lifecycle. But we can add those incrementally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, I guess I will add those here in this same PR.