sanity-plugin-transifex 4.0.3 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,90 +0,0 @@
1
- import {Adapter, Secrets} from 'sanity-translations-tab'
2
- import {baseTransifexUrl, getHeaders} from './helpers'
3
-
4
- const pollForFileDownloadLocation = async (
5
- resourceDownloadUrl: string,
6
- translationDownloadId: string,
7
- headers: Record<string, any>
8
- ): Promise<string> => {
9
- const response = await fetch(`${resourceDownloadUrl}/${translationDownloadId}`, {
10
- headers: headers,
11
- })
12
-
13
- if (response.status === 500) {
14
- //eslint-disable-next-line no-console -- this is for developer feedback/debugging
15
- console.info(
16
- `Transifex plugin message: Received 500 for translation download ID ${translationDownloadId}. Trying to reconnect...`
17
- )
18
- await new Promise((resolve) => setTimeout(resolve, 3000))
19
- return pollForFileDownloadLocation(resourceDownloadUrl, translationDownloadId, headers)
20
- } else if (response.redirected) {
21
- //eslint-disable-next-line no-console -- this is for developer feedback/debugging
22
- console.info(
23
- `Transifex plugin message: Received redirect for translation download ID ${translationDownloadId}. Following redirect now for file download.`
24
- )
25
- return response.url
26
- } else if (response.status === 200) {
27
- //eslint-disable-next-line no-console -- this is for developer feedback/debugging
28
- console.info(
29
- `Transifex plugin message: Requested download location for translation download ID ${translationDownloadId}. Location is still pending, trying again.`
30
- )
31
- await new Promise((resolve) => setTimeout(resolve, 3000))
32
- return pollForFileDownloadLocation(resourceDownloadUrl, translationDownloadId, headers)
33
- }
34
- //eslint-disable-next-line no-console -- this is for developer feedback/debugging
35
- console.error(
36
- `Transifex plugin message: Requested download location for translation download ID ${translationDownloadId} but received error code ${response.status}. Waiting and trying again.`
37
- )
38
- await new Promise((resolve) => setTimeout(resolve, 3000))
39
- return pollForFileDownloadLocation(resourceDownloadUrl, translationDownloadId, headers)
40
- }
41
-
42
- const handleFileDownload = (url: string) => {
43
- return fetch(url).then((res) => res.text())
44
- }
45
-
46
- export const getTranslation: Adapter['getTranslation'] = async (
47
- taskId: string,
48
- localeId: string,
49
- secrets: Secrets | null
50
- ) => {
51
- const resourceDownloadBody = {
52
- data: {
53
- attributes: {
54
- content_encoding: 'text',
55
- },
56
- relationships: {
57
- language: {
58
- data: {
59
- id: `l:${localeId}`,
60
- type: 'languages',
61
- },
62
- },
63
- resource: {
64
- data: {
65
- id: taskId,
66
- type: 'resources',
67
- },
68
- },
69
- },
70
- type: 'resource_translations_async_downloads',
71
- },
72
- }
73
-
74
- const resourceDownloadUrl = `${baseTransifexUrl}/resource_translations_async_downloads`
75
- const translationDownloadId = await fetch(resourceDownloadUrl, {
76
- headers: getHeaders(secrets),
77
- method: 'POST',
78
- body: JSON.stringify(resourceDownloadBody),
79
- })
80
- .then((res) => res.json())
81
- .then((res) => res.data.id)
82
-
83
- const headers = getHeaders(secrets)
84
- const location = await pollForFileDownloadLocation(
85
- resourceDownloadUrl,
86
- translationDownloadId,
87
- headers
88
- )
89
- return handleFileDownload(location)
90
- }
@@ -1,51 +0,0 @@
1
- import {Adapter, Secrets} from 'sanity-translations-tab'
2
- import {baseTransifexUrl, projOrgSlug, getHeaders} from './helpers'
3
- import {getLocales} from './getLocales'
4
-
5
- export const getTranslationTask: Adapter['getTranslationTask'] = async (
6
- documentId: string,
7
- secrets: Secrets | null
8
- ) => {
9
- if (!documentId || !secrets) {
10
- return {
11
- taskId: documentId,
12
- documentId: documentId,
13
- locales: [],
14
- }
15
- }
16
- const projectFilter = `filter[project]=${projOrgSlug(secrets)}`
17
- const resourceFilter = `filter[resource]=${projOrgSlug(secrets)}:r:${documentId}`
18
- const task = await fetch(
19
- `${baseTransifexUrl}/resource_language_stats?${projectFilter}&${resourceFilter}`,
20
- {headers: getHeaders(secrets)}
21
- )
22
- .then((res) => {
23
- if (res.ok) {
24
- return res.json()
25
- }
26
- //normal -- just means that this task doesn't exist yet.
27
- else if (res.status === 404) {
28
- return {data: []}
29
- }
30
- throw Error(`Failed to retrieve tasks from Transifex. Status: ${res.status}`)
31
- })
32
- .then((res) => ({
33
- taskId: `${projOrgSlug(secrets)}:r:${documentId}`,
34
- documentId: documentId,
35
- locales: res.data.map((locale: Record<string, any>) => ({
36
- localeId: locale.relationships.language.data.id.split(':')[1],
37
- progress: Math.floor(
38
- 100 * (locale.attributes.reviewed_strings / parseFloat(locale.attributes.total_strings))
39
- ),
40
- })),
41
- }))
42
-
43
- const locales = await getLocales(secrets)
44
- const localeIds = locales.map((l: Record<string, any>) => l.localeId)
45
- const validLocales = task.locales.filter((locale: Record<string, any>) =>
46
- localeIds.find((id: string) => id === locale.localeId)
47
- )
48
- task.locales = validLocales
49
-
50
- return task
51
- }
@@ -1,11 +0,0 @@
1
- import {Secrets} from 'sanity-translations-tab'
2
-
3
- export const baseTransifexUrl = 'https://rest.api.transifex.com'
4
-
5
- export const getHeaders = (secrets: Secrets | null): Record<string, string> => ({
6
- Authorization: `Bearer ${secrets?.token}`,
7
- 'Content-Type': 'application/vnd.api+json',
8
- })
9
-
10
- export const projOrgSlug = (secrets: Secrets | null): string =>
11
- `o:${secrets?.organization}:p:${secrets?.project}`
@@ -1,13 +0,0 @@
1
- import {Adapter} from 'sanity-translations-tab'
2
-
3
- import {getLocales} from './getLocales'
4
- import {getTranslationTask} from './getTranslationTask'
5
- import {getTranslation} from './getTranslation'
6
- import {createTask} from './createTask'
7
-
8
- export const TransifexAdapter: Adapter = {
9
- getLocales,
10
- getTranslationTask,
11
- createTask,
12
- getTranslation,
13
- }
@@ -1,11 +0,0 @@
1
- const {showIncompatiblePluginDialog} = require('@sanity/incompatible-plugin')
2
- const {name, version, sanityExchangeUrl} = require('./package.json')
3
-
4
- export default showIncompatiblePluginDialog({
5
- name: name,
6
- versions: {
7
- v3: version,
8
- v2: undefined,
9
- },
10
- sanityExchangeUrl,
11
- })