sanity-plugin-transifex 2.0.5 → 3.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.
Files changed (35) hide show
  1. package/LICENSE +2 -2
  2. package/README.md +74 -200
  3. package/dist/index.d.ts +52 -11
  4. package/dist/index.esm.js +192 -0
  5. package/dist/index.esm.js.map +1 -0
  6. package/dist/index.js +251 -7
  7. package/dist/index.js.map +1 -0
  8. package/package.json +87 -48
  9. package/sanity.json +8 -0
  10. package/src/index.ts +14 -19
  11. package/src/transifexAdapter/createTask.ts +12 -17
  12. package/src/transifexAdapter/getLocales.ts +10 -11
  13. package/src/transifexAdapter/getTranslation.ts +48 -60
  14. package/src/transifexAdapter/getTranslationTask.ts +12 -19
  15. package/src/transifexAdapter/helpers.ts +3 -3
  16. package/src/transifexAdapter/index.ts +5 -5
  17. package/v2-incompatible.js +11 -0
  18. package/.github/workflows/main.yml +0 -29
  19. package/.github/workflows/npm-publish.yml +0 -19
  20. package/dist/sanity-plugin-transifex.cjs.development.js +0 -903
  21. package/dist/sanity-plugin-transifex.cjs.development.js.map +0 -1
  22. package/dist/sanity-plugin-transifex.cjs.production.min.js +0 -2
  23. package/dist/sanity-plugin-transifex.cjs.production.min.js.map +0 -1
  24. package/dist/sanity-plugin-transifex.esm.js +0 -844
  25. package/dist/sanity-plugin-transifex.esm.js.map +0 -1
  26. package/dist/transifexAdapter/createTask.d.ts +0 -6
  27. package/dist/transifexAdapter/getLocales.d.ts +0 -2
  28. package/dist/transifexAdapter/getTranslation.d.ts +0 -2
  29. package/dist/transifexAdapter/getTranslationTask.d.ts +0 -6
  30. package/dist/transifexAdapter/helpers.d.ts +0 -7
  31. package/dist/transifexAdapter/index.d.ts +0 -2
  32. package/test/directives.test.ts +0 -155
  33. package/test/localeId.test.ts +0 -17
  34. package/test/mergeTranslation.test.ts +0 -113
  35. package/tsconfig.json +0 -35
@@ -1,11 +1,53 @@
1
- import { Secrets } from 'sanity-translations-tab'
2
- import { baseTransifexUrl, getHeaders } from './helpers'
1
+ import {Adapter, Secrets} from 'sanity-translations-tab'
2
+ import {baseTransifexUrl, getHeaders} from './helpers'
3
3
 
4
- export default async function getTranslation(
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 (
5
47
  taskId: string,
6
48
  localeId: string,
7
49
  secrets: Secrets | null
8
- ) {
50
+ ) => {
9
51
  const resourceDownloadBody = {
10
52
  data: {
11
53
  attributes: {
@@ -35,8 +77,8 @@ export default async function getTranslation(
35
77
  method: 'POST',
36
78
  body: JSON.stringify(resourceDownloadBody),
37
79
  })
38
- .then(res => res.json())
39
- .then(res => res.data.id)
80
+ .then((res) => res.json())
81
+ .then((res) => res.data.id)
40
82
 
41
83
  const headers = getHeaders(secrets)
42
84
  const location = await pollForFileDownloadLocation(
@@ -46,57 +88,3 @@ export default async function getTranslation(
46
88
  )
47
89
  return handleFileDownload(location)
48
90
  }
49
-
50
- const pollForFileDownloadLocation = async (
51
- resourceDownloadUrl: string,
52
- translationDownloadId: string,
53
- headers: Record<string, any>
54
- ): Promise<string> => {
55
- const response = await fetch(
56
- `${resourceDownloadUrl}/${translationDownloadId}`,
57
- {
58
- headers: headers,
59
- }
60
- )
61
-
62
- if (response.status === 500) {
63
- console.info(
64
- `Transifex plugin message: Received 500 for translation download ID ${translationDownloadId}. Trying to reconnect...`
65
- )
66
- await new Promise(resolve => setTimeout(resolve, 3000))
67
- return pollForFileDownloadLocation(
68
- resourceDownloadUrl,
69
- translationDownloadId,
70
- headers
71
- )
72
- } else if (response.redirected) {
73
- console.info(
74
- `Transifex plugin message: Received redirect for translation download ID ${translationDownloadId}. Following redirect now for file download.`
75
- )
76
- return response.url
77
- } else if (response.status === 200) {
78
- console.info(
79
- `Transifex plugin message: Requested download location for translation download ID ${translationDownloadId}. Location is still pending, trying again.`
80
- )
81
- await new Promise(resolve => setTimeout(resolve, 3000))
82
- return pollForFileDownloadLocation(
83
- resourceDownloadUrl,
84
- translationDownloadId,
85
- headers
86
- )
87
- } else {
88
- console.info(
89
- `Transifex plugin message: Requested download location for translation download ID ${translationDownloadId} but received error code ${response.status}. Waiting and trying again.`
90
- )
91
- await new Promise(resolve => setTimeout(resolve, 3000))
92
- return pollForFileDownloadLocation(
93
- resourceDownloadUrl,
94
- translationDownloadId,
95
- headers
96
- )
97
- }
98
- }
99
-
100
- const handleFileDownload = async (url: string) => {
101
- return fetch(url).then(res => res.text())
102
- }
@@ -1,11 +1,11 @@
1
- import { Secrets } from 'sanity-translations-tab'
2
- import { baseTransifexUrl, projOrgSlug, getHeaders } from './helpers'
3
- import getLocales from './getLocales'
1
+ import {Adapter, Secrets} from 'sanity-translations-tab'
2
+ import {baseTransifexUrl, projOrgSlug, getHeaders} from './helpers'
3
+ import {getLocales} from './getLocales'
4
4
 
5
- export default async function getTranslationTask(
5
+ export const getTranslationTask: Adapter['getTranslationTask'] = async (
6
6
  documentId: string,
7
7
  secrets: Secrets | null
8
- ) {
8
+ ) => {
9
9
  if (!documentId || !secrets) {
10
10
  return {
11
11
  taskId: documentId,
@@ -14,35 +14,28 @@ export default async function getTranslationTask(
14
14
  }
15
15
  }
16
16
  const projectFilter = `filter[project]=${projOrgSlug(secrets)}`
17
- const resourceFilter = `filter[resource]=${projOrgSlug(
18
- secrets
19
- )}:r:${documentId}`
17
+ const resourceFilter = `filter[resource]=${projOrgSlug(secrets)}:r:${documentId}`
20
18
  const task = await fetch(
21
19
  `${baseTransifexUrl}/resource_language_stats?${projectFilter}&${resourceFilter}`,
22
- { headers: getHeaders(secrets) }
20
+ {headers: getHeaders(secrets)}
23
21
  )
24
- .then(res => {
22
+ .then((res) => {
25
23
  if (res.ok) {
26
24
  return res.json()
27
25
  }
28
26
  //normal -- just means that this task doesn't exist yet.
29
27
  else if (res.status === 404) {
30
- return { data: [] }
31
- } else {
32
- throw Error(
33
- `Failed to retrieve tasks from Transifex. Status: ${res.status}`
34
- )
28
+ return {data: []}
35
29
  }
30
+ throw Error(`Failed to retrieve tasks from Transifex. Status: ${res.status}`)
36
31
  })
37
- .then(res => ({
32
+ .then((res) => ({
38
33
  taskId: `${projOrgSlug(secrets)}:r:${documentId}`,
39
34
  documentId: documentId,
40
35
  locales: res.data.map((locale: Record<string, any>) => ({
41
36
  localeId: locale.relationships.language.data.id.split(':')[1],
42
37
  progress: Math.floor(
43
- 100 *
44
- (locale.attributes.reviewed_strings /
45
- parseFloat(locale.attributes.total_strings))
38
+ 100 * (locale.attributes.reviewed_strings / parseFloat(locale.attributes.total_strings))
46
39
  ),
47
40
  })),
48
41
  }))
@@ -1,11 +1,11 @@
1
- import { Secrets } from 'sanity-translations-tab'
1
+ import {Secrets} from 'sanity-translations-tab'
2
2
 
3
3
  export const baseTransifexUrl = 'https://rest.api.transifex.com'
4
4
 
5
- export const getHeaders = (secrets: Secrets | null) => ({
5
+ export const getHeaders = (secrets: Secrets | null): Record<string, string> => ({
6
6
  Authorization: `Bearer ${secrets?.token}`,
7
7
  'Content-Type': 'application/vnd.api+json',
8
8
  })
9
9
 
10
- export const projOrgSlug = (secrets: Secrets | null) =>
10
+ export const projOrgSlug = (secrets: Secrets | null): string =>
11
11
  `o:${secrets?.organization}:p:${secrets?.project}`
@@ -1,9 +1,9 @@
1
- import { Adapter } from 'sanity-translations-tab'
1
+ import {Adapter} from 'sanity-translations-tab'
2
2
 
3
- import getLocales from './getLocales'
4
- import getTranslationTask from './getTranslationTask'
5
- import getTranslation from './getTranslation'
6
- import createTask from './createTask'
3
+ import {getLocales} from './getLocales'
4
+ import {getTranslationTask} from './getTranslationTask'
5
+ import {getTranslation} from './getTranslation'
6
+ import {createTask} from './createTask'
7
7
 
8
8
  export const TransifexAdapter: Adapter = {
9
9
  getLocales,
@@ -0,0 +1,11 @@
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
+ })
@@ -1,29 +0,0 @@
1
- name: CI
2
- on: [push]
3
- jobs:
4
- build:
5
- name: Build, lint, and test on Node ${{ matrix.node }} and ${{ matrix.os }}
6
-
7
- runs-on: ${{ matrix.os }}
8
- strategy:
9
- matrix:
10
- node: ['12.x', '14.x', '16.x']
11
- os: [ubuntu-latest, windows-latest, macOS-latest]
12
-
13
- steps:
14
- - name: Checkout repo
15
- uses: actions/checkout@v2
16
-
17
- - name: Use Node ${{ matrix.node }}
18
- uses: actions/setup-node@v1
19
- with:
20
- node-version: ${{ matrix.node }}
21
-
22
- - name: Install deps and build (with cache)
23
- uses: bahmutov/npm-install@v1
24
-
25
- - name: Lint
26
- run: yarn lint
27
-
28
- - name: Build
29
- run: yarn build
@@ -1,19 +0,0 @@
1
- name: Node.js Package
2
-
3
- on:
4
- release:
5
- types: [created]
6
-
7
- jobs:
8
- publish-npm:
9
- runs-on: ubuntu-latest
10
- steps:
11
- - uses: actions/checkout@v2
12
- - uses: actions/setup-node@v1
13
- with:
14
- node-version: 15
15
- registry-url: https://registry.npmjs.org/
16
- - run: npm ci
17
- - run: npm publish
18
- env:
19
- NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}