Skip to content

Commit

Permalink
fix: hotfix for custom job names (#23)
Browse files Browse the repository at this point in the history
* fix: hotfix for custom job names
  • Loading branch information
cngonzalez authored Sep 12, 2023
1 parent faa039b commit f3e7d3b
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 41 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sanity-plugin-studio-smartling",
"version": "4.0.1",
"version": "4.0.2",
"description": "!smartling gif",
"keywords": [
"sanity",
Expand Down
46 changes: 31 additions & 15 deletions src/adapter/createTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ import {Adapter, Secrets} from 'sanity-translations-tab'
import {getTranslationTask} from './getTranslationTask'
import {Buffer} from 'buffer'

const createJob = (jobName: string, secrets: Secrets, localeIds: string[], accessToken: string) => {
const createJob = (
jobName: string,
secrets: Secrets,
localeIds: string[],
accessToken: string,
documentId: string,
) => {
const {project, proxy} = secrets
if (!project || !proxy) {
throw new Error(
'The Smartling adapter requires a Smartling project identifier and a proxy URL. Please check your secrets document in this dataset, per the plugin documentation.'
'The Smartling adapter requires a Smartling project identifier and a proxy URL. Please check your secrets document in this dataset, per the plugin documentation.',
)
}

Expand All @@ -21,6 +27,7 @@ const createJob = (jobName: string, secrets: Secrets, localeIds: string[], acces
body: JSON.stringify({
jobName,
targetLocaleIds: localeIds,
referenceNumber: documentId,
}),
})
.then((res) => res.json())
Expand All @@ -36,16 +43,16 @@ const createJob = (jobName: string, secrets: Secrets, localeIds: string[], acces
const createJobBatch = (
jobId: string,
secrets: Secrets,
documentName: string,
documentId: string,
accessToken: string,
localeIds: string[],
workflowUid?: string
workflowUid?: string,
//eslint-disable-next-line max-params
) => {
const {project, proxy} = secrets
if (!project || !proxy) {
throw new Error(
'The Smartling adapter requires a Smartling project identifier and a proxy URL. Please check your secrets document in this dataset, per the plugin documentation.'
'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/job-batches-api/v2/projects/${project}/batches`
Expand All @@ -57,7 +64,7 @@ const createJobBatch = (
} = {
authorize: true,
translationJobUid: jobId,
fileUris: [documentName],
fileUris: [documentId],
}

if (workflowUid) {
Expand All @@ -81,20 +88,22 @@ const createJobBatch = (

const uploadFileToBatch = (
batchUid: string,
documentId: string,
document: Record<string, any>,
secrets: Secrets,
localeIds: string[],
accessToken: string
accessToken: string,
//eslint-disable-next-line max-params
) => {
const {project, proxy} = secrets
if (!project || !proxy) {
throw new Error(
'The Smartling adapter requires a Smartling project identifier and a proxy URL. Please check your secrets document in this dataset, per the plugin documentation.'
'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/job-batches-api/v2/projects/${project}/batches/${batchUid}/file`
const formData = new FormData()
formData.append('fileUri', document.name)
formData.append('fileUri', documentId)
formData.append('fileType', 'html')
const htmlBuffer = Buffer.from(document.content, 'utf-8')
formData.append('file', new Blob([htmlBuffer]), `${document.name}.html`)
Expand All @@ -112,30 +121,37 @@ export const createTask: Adapter['createTask'] = async (
document: Record<string, any>,
localeIds: string[],
secrets: Secrets | null,
workflowUid?: string
workflowUid?: string,
) => {
if (!secrets?.project || !secrets?.secret || !secrets?.proxy) {
throw new Error(
'The Smartling adapter requires a project ID, a secret key, and a proxy URL. Please check your secrets document in this dataset, per the plugin documentation.'
'The Smartling adapter requires a project ID, a secret key, and a proxy URL. Please check your secrets document in this dataset, per the plugin documentation.',
)
}

const accessToken = await authenticate(secrets)

let taskId = await findExistingJob(document.name, secrets, accessToken)
if (!taskId) {
taskId = await createJob(document.name, secrets, localeIds, accessToken)
taskId = await createJob(document.name, secrets, localeIds, accessToken, documentId)
}

const batchUid = await createJobBatch(
taskId,
secrets,
document.name,
documentId,
accessToken,
localeIds,
workflowUid
workflowUid,
)
const uploadFileRes = await uploadFileToBatch(
batchUid,
documentId,
document,
secrets,
localeIds,
accessToken,
)
const uploadFileRes = await uploadFileToBatch(batchUid, document, secrets, localeIds, accessToken)
//eslint-disable-next-line no-console -- for developer debugging
console.info('Upload status from Smartling: ', uploadFileRes)

Expand Down
4 changes: 2 additions & 2 deletions src/adapter/getTranslation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import {Adapter, Secrets} from 'sanity-translations-tab'
export const getTranslation: Adapter['getTranslation'] = async (
taskId: string,
localeId: string,
secrets: Secrets | null
secrets: Secrets | null,
) => {
if (!secrets?.project || !secrets?.secret || !secrets?.proxy) {
throw new Error(
'The Smartling adapter requires a project ID, a secret key, and a proxy URL. Please check your secrets document in this dataset, per the plugin documentation.'
'The Smartling adapter requires a project ID, a secret key, and a proxy URL. Please check your secrets document in this dataset, per the plugin documentation.',
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/adapter/getTranslationTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface SmartlingProgressItem {

export const getTranslationTask: Adapter['getTranslationTask'] = async (
documentId: string,
secrets: Secrets | null
secrets: Secrets | null,
) => {
if (!secrets?.project || !secrets?.secret || !secrets?.proxy) {
return {
Expand Down
55 changes: 37 additions & 18 deletions src/adapter/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const authenticate = (secrets: Secrets): Promise<string> => {
const {secret, proxy} = secrets
if (!secret || !proxy) {
throw new Error(
'The Smartling adapter requires a secret key and a proxy URL. Please check your secrets document in this dataset, per the plugin documentation.'
'The Smartling adapter requires a secret key and a proxy URL. Please check your secrets document in this dataset, per the plugin documentation.',
)
}
return fetch(proxy, {
Expand All @@ -30,34 +30,53 @@ export const getHeaders = (url: string, accessToken: string): Headers => ({
'X-URL': url,
})

export const findExistingJob = (
export const findExistingJob = async (
documentId: string,
secrets: Secrets,
accessToken: string
accessToken: string,
): Promise<string> => {
const {project, proxy} = secrets
if (!project || !proxy) {
throw new Error(
'The Smartling adapter requires a Smartling project identifier and a proxy URL. Please check your secrets document in this dataset, per the plugin documentation.'
'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}`
return fetch(proxy, {
method: 'POST',
//first, try fetching from name resolution
let items = await fetch(proxy, {
headers: getHeaders(url, accessToken),
})
.then((res) => res.json())
.then((res) => {
if (res.response.data.items.length) {
//smartling will fuzzy match job names. We need to be precise.
const correctJob = res.response.data.items.find(
(item: {jobName: string}) => item.jobName && item.jobName === documentId
)
if (correctJob) {
return correctJob.translationJobUid
}
return ''
}
return ''
.then((res) => res?.response?.data?.items)

if (!items || !items.length) {
//if that fails, try fetching by fileUri and check the referenceNumber
const refUrl = `https://api.smartling.com/jobs-api/v3/projects/${project}/jobs/search`
items = await fetch(proxy, {
headers: {
...getHeaders(refUrl, accessToken),
'content-type': 'application/json',
},
method: 'POST',
body: JSON.stringify({
fileUris: [documentId],
}),
})
.then((res) => res.json())
.then((res) => res?.response?.data?.items)
}

if (items.length) {
//smartling will fuzzy match job names. We need to be precise.
const correctJob = items.find(
(item: {jobName: string; referenceNumber: string}) =>
(item.jobName && item.jobName === documentId) ||
(item.referenceNumber && item.referenceNumber === documentId),
)

if (correctJob) {
return correctJob.translationJobUid
}
}
return ''
}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ interface ConfigOptions {
secretsNamespace: string | null
exportForTranslation: (
id: string,
context: TranslationFunctionContext
context: TranslationFunctionContext,
) => Promise<SerializedDocument>
importTranslation: (
id: string,
localeId: string,
doc: string,
context: TranslationFunctionContext
context: TranslationFunctionContext,
) => Promise<void>
}
const defaultDocumentLevelConfig: ConfigOptions = {
Expand Down

0 comments on commit f3e7d3b

Please sign in to comment.