-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-import.ts
60 lines (54 loc) · 2 KB
/
api-import.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { RequestConfig } from '@/types/request-config';
import { ApiBase } from '@/api/methods/api-base';
import { File } from '@/types/file';
import { I18nJson } from '@/types/i18n-json';
import { ImportData } from '@/types/import-data';
import { importDataFactory } from '@/types/import-data-factory';
import { ImportDataFile } from '@/types/import-data-file';
import { ImportJsonRequest } from '@/types/import-json-request';
import { Project } from '@/types/project';
import { delay } from '@/utils/delay';
import { JsonUtils } from '@/utils/json-utils';
export class ApiImport extends ApiBase {
/**
* Import source keys from JSON object.
*
* @param request Import JSON request config.
* @param config Request config.
*
* @see {@link https://localazy.com/docs/api/import#import-content-to-a-project Localazy API Docs}
*/
public async json(
request: ImportJsonRequest,
config?: RequestConfig,
): Promise<ReturnType<ApiImport['getImportedFile']>> {
const { project, json }: ImportJsonRequest = request;
const projectId: string = ApiBase.getId(project, 'project');
const chunks: I18nJson[] = JsonUtils.slice(json);
const data: ImportData = importDataFactory(request, chunks);
const { result }: { result: string } = (await this.api.client.post(
`/projects/${projectId}/import`,
data,
config,
)) as { result: string };
await delay();
return this.getImportedFile(project, data, result);
}
protected async getImportedFile(
project: string | Project,
data: ImportData,
importBatch: string,
): Promise<File & { importBatch: string }> {
const files: File[] = await this.api.files.list({ project });
const file: File | undefined = files.find((f: File): boolean =>
data.files.some(
(importFile: ImportDataFile): boolean =>
f.name === (importFile.name || 'content.json') && f.path === importFile.path,
),
);
return {
...file,
importBatch,
} as File & { importBatch: string };
}
}