From 598c993709e6d3b62748c28544d1d4a09117182e Mon Sep 17 00:00:00 2001 From: arthurpereira Date: Mon, 9 Oct 2023 15:33:20 -0300 Subject: [PATCH 1/7] fix: do not use document id as job name filter --- src/adapter/helpers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/adapter/helpers.ts b/src/adapter/helpers.ts index 24467e8..31b6a29 100644 --- a/src/adapter/helpers.ts +++ b/src/adapter/helpers.ts @@ -41,7 +41,7 @@ export const findExistingJob = async ( 'The Smartling adapter requires a Smartling project identifier and a proxy URL. Please check your secrets document in this dataset, per the plugin documentation.', ) } - const url = `https://api.smartling.com/jobs-api/v3/projects/${project}/jobs?jobName=${documentId}` + const url = `https://api.smartling.com/jobs-api/v3/projects/${project}/jobs` //first, try fetching from name resolution let items = await fetch(proxy, { headers: getHeaders(url, accessToken), From 67a692768f14a75e7270a71b0e7516c786a3a651 Mon Sep 17 00:00:00 2001 From: arthurpereira Date: Tue, 10 Oct 2023 09:47:11 -0300 Subject: [PATCH 2/7] refactor: receive callbackUrl as option --- src/adapter/createTask.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/adapter/createTask.ts b/src/adapter/createTask.ts index d8039a1..4df61ce 100644 --- a/src/adapter/createTask.ts +++ b/src/adapter/createTask.ts @@ -93,6 +93,7 @@ const uploadFileToBatch = ( secrets: Secrets, localeIds: string[], accessToken: string, + customParams?: {callbackUrl: string}, //eslint-disable-next-line max-params ) => { const {project, proxy} = secrets @@ -109,6 +110,10 @@ const uploadFileToBatch = ( formData.append('file', new Blob([htmlBuffer]), `${document.name}.html`) localeIds.forEach((localeId) => formData.append('localeIdsToAuthorize[]', localeId)) + if (customParams?.callbackUrl) { + formData.append('callbackUrl', customParams?.callbackUrl) + } + return fetch(proxy, { method: 'POST', headers: getHeaders(url, accessToken), @@ -122,6 +127,8 @@ export const createTask: Adapter['createTask'] = async ( localeIds: string[], secrets: Secrets | null, workflowUid?: string, + customParams?: {callbackUrl: string}, + // eslint-disable-next-line max-params ) => { if (!secrets?.project || !secrets?.secret || !secrets?.proxy) { throw new Error( @@ -151,6 +158,7 @@ export const createTask: Adapter['createTask'] = async ( secrets, localeIds, accessToken, + customParams, ) //eslint-disable-next-line no-console -- for developer debugging console.info('Upload status from Smartling: ', uploadFileRes) From a7a7b3396bc9ffbb7eb913e4ae2a926bb605fd1b Mon Sep 17 00:00:00 2001 From: arthurpereira Date: Tue, 10 Oct 2023 09:48:40 -0300 Subject: [PATCH 3/7] fix: import translated content back --- src/adapter/getTranslation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/adapter/getTranslation.ts b/src/adapter/getTranslation.ts index 4b5708f..7e8190a 100644 --- a/src/adapter/getTranslation.ts +++ b/src/adapter/getTranslation.ts @@ -14,7 +14,7 @@ export const getTranslation: Adapter['getTranslation'] = async ( const {project, proxy} = secrets - const url = `https://api.smartling.com/files-api/v2/projects/${project}/locales/${localeId}/file?fileUri=${taskId}&retrievalType=pending` + const url = `https://api.smartling.com/files-api/v2/projects/${project}/locales/${localeId}/file?fileUri=${localeId}_${taskId}&retrievalType=pending` const accessToken = await authenticate(secrets) const translatedHTML = await fetch(proxy, { method: 'GET', From 324f0a0f5ed750d4b3d8585990ef4ac40abe770a Mon Sep 17 00:00:00 2001 From: arthurpereira Date: Tue, 10 Oct 2023 10:54:24 -0300 Subject: [PATCH 4/7] refactor: use CustomParams type --- src/adapter/createTask.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/adapter/createTask.ts b/src/adapter/createTask.ts index 4df61ce..9535750 100644 --- a/src/adapter/createTask.ts +++ b/src/adapter/createTask.ts @@ -1,5 +1,5 @@ import {authenticate, getHeaders, findExistingJob} from './helpers' -import {Adapter, Secrets} from 'sanity-translations-tab' +import {Adapter, Secrets, CustomParams} from 'sanity-translations-tab' import {getTranslationTask} from './getTranslationTask' import {Buffer} from 'buffer' @@ -93,7 +93,7 @@ const uploadFileToBatch = ( secrets: Secrets, localeIds: string[], accessToken: string, - customParams?: {callbackUrl: string}, + customParams?: CustomParams, //eslint-disable-next-line max-params ) => { const {project, proxy} = secrets @@ -127,7 +127,7 @@ export const createTask: Adapter['createTask'] = async ( localeIds: string[], secrets: Secrets | null, workflowUid?: string, - customParams?: {callbackUrl: string}, + customParams?: CustomParams, // eslint-disable-next-line max-params ) => { if (!secrets?.project || !secrets?.secret || !secrets?.proxy) { From 2d44cc70e8eb01782b70f79640d3430a1e3bc067 Mon Sep 17 00:00:00 2001 From: arthurpereira Date: Tue, 10 Oct 2023 16:53:18 -0300 Subject: [PATCH 5/7] docs: update docs --- docs/01-advanced-configuration.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/01-advanced-configuration.md b/docs/01-advanced-configuration.md index 9be44ff..58e9305 100644 --- a/docs/01-advanced-configuration.md +++ b/docs/01-advanced-configuration.md @@ -54,6 +54,13 @@ const customConfig = { }, }, ] + }, + + // add custom params for Smartling + customParams: { + // GET request that creates a callback to a URL when a file is 100% published for a locale. + // The callback gives the fileUri and locale with the format http[/s]://your.url?locale=xx-XX&fileUri=your.file. + callbackUrl: 'https://your-endpoint.here' } //adapter, baseLanguage, secretsNamespace, importTranslation, exportForTranslation should likely not be touched unless you very much want to customize your plugin. } satisfies TranslationsTabConfigOptions From 98f355712d5435c5aa3def3ad98c8f97c7036281 Mon Sep 17 00:00:00 2001 From: arthurpereira Date: Tue, 10 Oct 2023 18:06:06 -0300 Subject: [PATCH 6/7] refactor: use callbackUrl as function --- src/adapter/createTask.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/adapter/createTask.ts b/src/adapter/createTask.ts index 9535750..3b2811c 100644 --- a/src/adapter/createTask.ts +++ b/src/adapter/createTask.ts @@ -110,8 +110,8 @@ const uploadFileToBatch = ( formData.append('file', new Blob([htmlBuffer]), `${document.name}.html`) localeIds.forEach((localeId) => formData.append('localeIdsToAuthorize[]', localeId)) - if (customParams?.callbackUrl) { - formData.append('callbackUrl', customParams?.callbackUrl) + if (customParams?.callbackUrl && typeof customParams?.callbackUrl === 'function') { + formData.append('callbackUrl', customParams?.callbackUrl(document)) } return fetch(proxy, { From 77b403bc5c25c99a2cc79e3e5a9f2d68d5de064f Mon Sep 17 00:00:00 2001 From: arthurpereira Date: Tue, 10 Oct 2023 18:30:20 -0300 Subject: [PATCH 7/7] fix: remove locale from fileUri --- src/adapter/getTranslation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/adapter/getTranslation.ts b/src/adapter/getTranslation.ts index 7e8190a..4b5708f 100644 --- a/src/adapter/getTranslation.ts +++ b/src/adapter/getTranslation.ts @@ -14,7 +14,7 @@ export const getTranslation: Adapter['getTranslation'] = async ( const {project, proxy} = secrets - const url = `https://api.smartling.com/files-api/v2/projects/${project}/locales/${localeId}/file?fileUri=${localeId}_${taskId}&retrievalType=pending` + const url = `https://api.smartling.com/files-api/v2/projects/${project}/locales/${localeId}/file?fileUri=${taskId}&retrievalType=pending` const accessToken = await authenticate(secrets) const translatedHTML = await fetch(proxy, { method: 'GET',