-
Notifications
You must be signed in to change notification settings - Fork 117
Add helper method for creating and uploading a Webflow Asset #251
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 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9a60f8c
[draft] add asset create and upload utility helper
zplata 4f77dfb
fix asset upload to s3
zplata 86cfe2a
Add tests for asset upload utility method
zplata 9cb4037
fix testing command
zplata 5fcaa3c
review feedback cleanup
zplata 0a283f9
fix tests
zplata 37d33fb
bump version to 3.1.2
zplata 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
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
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,28 @@ | ||
| import { Assets } from "../api/resources/assets/client/Client"; | ||
| import { Client as Utilities } from "./AssetsUtilitiesClient"; | ||
|
|
||
| // Extends the namespace declared in the Fern generated client | ||
| declare module "../api/resources/assets/client/Client" { | ||
| export namespace Assets { | ||
| // interface RequestSignatureDetails { | ||
| // /** The headers of the incoming webhook request as a record-like object */ | ||
| // headers: Record<string, string>; | ||
| // /** The body of the incoming webhook request as a string */ | ||
| // body: string; | ||
| // /** The secret key generated when creating the webhook or the OAuth client secret */ | ||
| // secret: string; | ||
| // } | ||
| } | ||
| } | ||
|
|
||
| export class Client extends Assets { | ||
| constructor(protected readonly _options: Assets.Options) { | ||
| super(_options); | ||
| } | ||
|
|
||
| protected _utilities: Utilities | undefined; | ||
|
|
||
| public get utilities(): Utilities { | ||
| return (this._utilities ??= new Utilities(this._options)); | ||
| } | ||
| } |
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,156 @@ | ||
| import * as Webflow from "../api"; | ||
| import { Assets } from "../api/resources/assets/client/Client"; | ||
| import * as core from "../core"; | ||
| import * as environments from "../environments"; | ||
| import crypto from "crypto"; | ||
| import fetch from "node-fetch"; | ||
| import FormDataConstructor from 'form-data'; | ||
|
|
||
| export declare namespace AssetsUtilities { | ||
| interface Options { | ||
| environment?: core.Supplier<environments.WebflowEnvironment | string>; | ||
| accessToken: core.Supplier<core.BearerToken>; | ||
| } | ||
|
|
||
| interface RequestOptions { | ||
| /** The maximum time to wait for a response in seconds. */ | ||
| timeoutInSeconds?: number; | ||
| /** The number of times to retry the request. Defaults to 2. */ | ||
| maxRetries?: number; | ||
| /** A hook to abort the request. */ | ||
| abortSignal?: AbortSignal; | ||
| /** Additional headers to include in the request. */ | ||
| headers?: Record<string, string>; | ||
| } | ||
| } | ||
|
|
||
| interface TestAssetUploadRequest { | ||
| /** | ||
| * File to upload via URL where the asset is hosted, or by ArrayBuffer | ||
| */ | ||
| file: ArrayBuffer | string; | ||
|
|
||
| /** | ||
| * Name of the file to upload, including the extension | ||
| */ | ||
| fileName: string; | ||
|
|
||
| /** | ||
| * Name of the parent folder to upload to | ||
| */ | ||
| parentFolder?: string; | ||
| } | ||
|
|
||
| // Utilities class for Assets to add custom helper methods to assist in managing Webflow Assets | ||
| export class Client extends Assets { | ||
| constructor(protected readonly _options: AssetsUtilities.Options) { | ||
| super(_options); | ||
| } | ||
|
|
||
| private async _getBufferFromUrl(url: string): Promise<ArrayBuffer> { | ||
| try { | ||
| const response = await fetch(url); | ||
| if (!response.ok) { | ||
| throw new Error(`Failed to fetch asset from URL: ${url}. Status: ${response.status} ${response.statusText}`); | ||
| } | ||
| return await response.arrayBuffer(); | ||
| } catch (error) { | ||
| throw new Error(`Error occurred while fetching asset from URL: ${url}. ${(error as Error).message}`); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Create the Asset metadata in Webflow, and immediately upload it to the S3 bucket on behalf of the user to simplify the 2-step process | ||
| * | ||
| * @param siteId | ||
| * @param request | ||
| * @param requestOptions | ||
| * @returns | ||
| */ | ||
| public async createAndUpload( | ||
| siteId: string, | ||
| request: TestAssetUploadRequest, | ||
| requestOptions?: Assets.RequestOptions | ||
| ): Promise<Webflow.AssetUpload> { | ||
| /** 1. Generate the hash */ | ||
| const {file, fileName, parentFolder} = request; | ||
| let tempBuffer: Buffer | null = null; | ||
| if (typeof file === 'string') { | ||
| const arrBuffer = await this._getBufferFromUrl(file); | ||
| tempBuffer = Buffer.from(arrBuffer); | ||
| } else if (file instanceof ArrayBuffer) { | ||
| tempBuffer = Buffer.from(file); | ||
| } | ||
| if (tempBuffer === null) { | ||
| throw new Error('Invalid file'); | ||
| } | ||
| const hash = crypto.createHash("md5").update(Buffer.from(tempBuffer)).digest("hex"); | ||
zplata marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const wfUploadRequest = { | ||
| fileName, | ||
| fileHash: hash, | ||
| } as Webflow.AssetsCreateRequest; | ||
| if (parentFolder) { | ||
| wfUploadRequest["parentFolder"] = parentFolder; | ||
| } | ||
zplata marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| /** 2. Create the Asset Metadata in Webflow */ | ||
| let wfUploadedAsset: Webflow.AssetUpload; | ||
| try { | ||
| wfUploadedAsset = await this.create(siteId, wfUploadRequest, requestOptions); | ||
| } catch (error) { | ||
| throw new Error(`Failed to create Asset metadata in Webflow: ${(error as Error).message}`); | ||
| } | ||
|
|
||
| /** 3. Create FormData with S3 bucket signature */ | ||
| const wfUploadDetails = wfUploadedAsset.uploadDetails!; | ||
zplata marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const uploadUrl = wfUploadedAsset.uploadUrl as string; | ||
| // Temp workaround since headers from response are being camelCased and we need them to be exact when sending to S3 | ||
| const headerMappings = { | ||
| 'xAmzAlgorithm': 'X-Amz-Algorithm', | ||
| 'xAmzDate': 'X-Amz-Date', | ||
| 'xAmzCredential': 'X-Amz-Credential', | ||
| 'xAmzSignature': 'X-Amz-Signature', | ||
| 'successActionStatus': 'success_action_status', | ||
| 'contentType': 'Content-Type', | ||
| 'cacheControl': 'Cache-Control', | ||
| }; | ||
| const transformedUploadHeaders = Object.keys(wfUploadDetails).reduce((acc: Record<string, any>, key) => { | ||
| const mappedKey = headerMappings[key as keyof typeof headerMappings] || key; | ||
| acc[mappedKey] = wfUploadDetails[key as keyof typeof headerMappings ]; | ||
| return acc; | ||
| }, {}); | ||
| const formDataToUpload = new FormDataConstructor(); | ||
| Object.keys(transformedUploadHeaders).forEach((key) => { | ||
| formDataToUpload.append(key, transformedUploadHeaders[key]); | ||
| }); | ||
|
|
||
| if (!Buffer.isBuffer(tempBuffer)) { | ||
zplata marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| throw new Error("Invalid Buffer: Expected a Buffer instance from file"); | ||
| } | ||
|
|
||
| formDataToUpload.append("file", tempBuffer, { | ||
| filename: fileName, | ||
| contentType: wfUploadedAsset.contentType || "application/octet-stream", | ||
| }); | ||
|
|
||
| /** 4. Upload to S3 */ | ||
| try { | ||
| const response = await fetch(uploadUrl, { | ||
| method: 'POST', | ||
| body: formDataToUpload, | ||
| headers: { ...formDataToUpload.getHeaders() }, | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| const errorText = await response.text(); | ||
| throw new Error(`Failed to upload to S3. Status: ${response.status}, Response: ${errorText}`); | ||
| } | ||
| } catch (error) { | ||
| throw new Error(`Error occurred during S3 upload: ${(error as Error).message}`); | ||
| } | ||
|
|
||
| return wfUploadedAsset; | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.