-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathFileService.ts
110 lines (104 loc) · 2.47 KB
/
FileService.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { Options } from '../client.js';
import { clientErrorWrapper } from '../utils/errors.js';
import {
FileServiceCreateInput,
FileServiceCreateOutput,
FileServiceDeleteInput,
FileServiceDeleteOutput,
FileServiceListInput,
FileServiceListOutput,
FileServiceReadInput,
FileServiceReadOutput,
FileServiceRetrieveInput,
FileServiceRetrieveOutput,
} from '../schema.js';
import { BaseService } from './BaseService.js';
export class FileService extends BaseService {
async create(
input: FileServiceCreateInput,
opts?: Options,
): Promise<FileServiceCreateOutput> {
return clientErrorWrapper(
this._client.POST('/v2/files', {
...opts,
body: { ...input, file: input.file.content }, // file is supplied just to avoid typecast
bodySerializer(body) {
const formData = new FormData();
formData.append('purpose', body.purpose);
formData.append('file', input.file.content, input.file.name);
return formData;
},
params: {
query: {
version: '2024-05-13',
},
},
}),
);
}
async retrieve(
input: FileServiceRetrieveInput,
opts?: Options,
): Promise<FileServiceRetrieveOutput> {
return clientErrorWrapper(
this._client.GET('/v2/files/{id}', {
...opts,
params: {
path: input,
query: {
version: '2024-05-13',
},
},
}),
);
}
async read(
input: FileServiceReadInput,
opts?: Options,
): Promise<FileServiceReadOutput> {
return clientErrorWrapper(
this._client.GET('/v2/files/{id}/content', {
...opts,
params: {
path: input,
query: {
version: '2023-11-22',
},
},
parseAs: 'blob',
}),
);
}
async delete(
input: FileServiceDeleteInput,
opts?: Options,
): Promise<FileServiceDeleteOutput> {
return clientErrorWrapper(
this._client.DELETE('/v2/files/{id}', {
...opts,
params: {
path: input,
query: {
version: '2023-11-22',
},
},
}),
);
}
async list(
input: FileServiceListInput,
opts?: Options,
): Promise<FileServiceListOutput> {
return clientErrorWrapper(
this._client.GET('/v2/files', {
...opts,
params: {
query: {
...input,
version: '2024-05-13',
},
},
}),
);
}
}