diff --git a/sasjs-tests/vite.config.js b/sasjs-tests/vite.config.js index 44484a7..5834b10 100644 --- a/sasjs-tests/vite.config.js +++ b/sasjs-tests/vite.config.js @@ -1,5 +1,12 @@ import { defineConfig } from 'vite' +import { nodePolyfills } from 'vite-plugin-node-polyfills' + export default defineConfig({ + plugins: [ + nodePolyfills({ + include: ['buffer'] + }) + ], build: { assetsInlineLimit: 0, assetsDir: '' diff --git a/src/SASViyaApiClient.ts b/src/SASViyaApiClient.ts index c89375b..473c773 100644 --- a/src/SASViyaApiClient.ts +++ b/src/SASViyaApiClient.ts @@ -481,11 +481,14 @@ export class SASViyaApiClient { if (!parentFolderUri && parentFolderPath) { parentFolderUri = await this.getFolderUri(parentFolderPath, accessToken) } - const headers = { Accept: 'application/vnd.sas.file+json', 'Content-Disposition': `filename="${fileName}";` } + const patchHeaders = { + Accept: 'application/json', + 'If-Match': '*' + } const formData = new NodeFormData() formData.append('file', contentBuffer, fileName) @@ -529,7 +532,8 @@ export class SASViyaApiClient { .forEach((e) => { e.extensions?.forEach((ext) => { this.fileExtensionMap.set(ext, { - typeDefName: e.name, // "name:" is the typeDefName value required for file creation. + // "name:" is the typeDefName value required for file creation. + typeDefName: e.name, properties: e.properties }) }) @@ -546,34 +550,38 @@ export class SASViyaApiClient { } } - return ( - await this.requestClient - .post( - `/files/files?parentFolderUri=${parentFolderUri}&typeDefName=${ - typeDefName ?? 'file' - }#rawUpload`, - formData, - accessToken, - 'multipart/form-data; boundary=' + (formData as any)._boundary, - headers - ) - .then(async (res) => { - // If a patch was created... - if (filePatch) { - // Get the URI of the newly created file - const fileUri = res.result.links.filter( - (e) => e.method == 'PATCH' && e.rel == 'patch' - )[0].uri - // and apply the patch - return await this.requestClient.patch( - `${fileUri}`, - filePatch, - accessToken - ) - } - return res - }) - ).result + const createFileResponse = await this.requestClient.post( + `/files/files?parentFolderUri=${parentFolderUri}&typeDefName=${ + typeDefName ?? 'file' + }#rawUpload`, + formData, + accessToken, + 'multipart/form-data; boundary=' + (formData as any)._boundary, + headers + ) + + try { + // If a patch was created... + if (filePatch) { + // Get the URI of the newly created file + const fileUri = createFileResponse.result.links.filter( + (e) => e.method == 'PATCH' && e.rel == 'patch' + )[0].uri + // and apply the patch + return ( + await this.requestClient.patch( + `${fileUri}`, + filePatch, + accessToken, + 'application/json', + patchHeaders + ) + ).result + } + return createFileResponse.result + } catch (e: any) { + throw new Error(`Error patching file ${fileName}.\n${e.message}`) + } } /** diff --git a/src/request/RequestClient.ts b/src/request/RequestClient.ts index c0295ed..f8cb470 100644 --- a/src/request/RequestClient.ts +++ b/src/request/RequestClient.ts @@ -272,10 +272,15 @@ export class RequestClient implements HttpClient { public async patch( url: string, - data: any = {}, - accessToken?: string + data: any, + accessToken: string | undefined, + contentType = 'application/json', + overrideHeaders: { [key: string]: string | number } = {} ): Promise<{ result: T; etag: string }> { - const headers = this.getHeaders(accessToken, 'application/json') + const headers = { + ...this.getHeaders(accessToken, contentType), + ...overrideHeaders + } return this.httpClient .patch(url, data, { headers, withXSRFToken: true })