Skip to content

Commit 97cbb2c

Browse files
committed
refactor deno templates
1 parent 6d2c065 commit 97cbb2c

File tree

4 files changed

+129
-126
lines changed

4 files changed

+129
-126
lines changed

templates/deno/base/params.twig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
let payload: Payload = {};
2+
3+
{% for parameter in method.parameters.query %}
4+
if (typeof {{ parameter.name | caseCamel | escapeKeyword }} !== 'undefined') {
5+
payload['{{ parameter.name }}'] = {{ parameter.name | caseCamel | escapeKeyword }}{% if method.consumes[0] == "multipart/form-data" and ( parameter.type != "string" and parameter.type != "array" and parameter.type != "file" ) %}.toString(){% endif %};
6+
}
7+
8+
{% endfor %}
9+
{% for parameter in method.parameters.body %}
10+
if (typeof {{ parameter.name | caseCamel | escapeKeyword }} !== 'undefined') {
11+
payload['{{ parameter.name }}'] = {{ parameter.name | caseCamel | escapeKeyword }}{% if method.consumes[0] == "multipart/form-data" and ( parameter.type != "string" and parameter.type != "array" and parameter.type != "file" ) %}.toString(){% endif %};
12+
}
13+
{% endfor %}

templates/deno/base/requests/api.twig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
return await this.client.call('{{ method.method | caseLower }}', path, {
2+
{% for parameter in method.parameters.header %}
3+
'{{ parameter.name }}': ${{ parameter.name | caseCamel | escapeKeyword }},
4+
{% endfor %}
5+
{% for key, header in method.headers %}
6+
'{{ key }}': '{{ header }}',
7+
{% endfor %}
8+
}, payload);
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
{% for parameter in method.parameters.all %}
2+
{% if parameter.type == 'file' %}
3+
4+
const size = {{ parameter.name | caseCamel | escapeKeyword }}.size;
5+
6+
const headers: { [header: string]: string } = {
7+
{% for parameter in method.parameters.header %}
8+
'{{ parameter.name }}': ${{ parameter.name | caseCamel | escapeKeyword }},
9+
{% endfor %}
10+
{% for key, header in method.headers %}
11+
'{{ key }}': '{{ header }}',
12+
{% endfor %}
13+
};
14+
15+
let id: string | undefined = undefined;
16+
let response: any = undefined;
17+
18+
let chunksUploaded = 0;
19+
20+
{% for parameter in method.parameters.all %}
21+
{% if parameter.isUploadID %}
22+
if({{ parameter.name | caseCamel | escapeKeyword }} != 'unique()') {
23+
try {
24+
response = await this.client.call('get', path + '/' + {{ parameter.name }}, headers);
25+
chunksUploaded = response.chunksUploaded;
26+
} catch(e) {
27+
}
28+
}
29+
{% endif %}
30+
{% endfor %}
31+
32+
let currentChunk = 1;
33+
let currentPosition = 0;
34+
let uploadableChunk = new Uint8Array(Client.CHUNK_SIZE);
35+
36+
const uploadChunk = async (lastUpload = false) => {
37+
if(currentChunk <= chunksUploaded) {
38+
return;
39+
}
40+
41+
const start = ((currentChunk - 1) * Client.CHUNK_SIZE);
42+
const end = start + currentPosition;
43+
44+
if(!lastUpload || currentChunk !== 1) {
45+
headers['content-range'] = 'bytes ' + start + '-' + end + '/' + size;
46+
}
47+
48+
let uploadableChunkTrimmed: Uint8Array;
49+
50+
if(currentPosition + 1 >= Client.CHUNK_SIZE) {
51+
uploadableChunkTrimmed = uploadableChunk;
52+
} else {
53+
uploadableChunkTrimmed = new Uint8Array(currentPosition);
54+
for(let i = 0; i <= currentPosition; i++) {
55+
uploadableChunkTrimmed[i] = uploadableChunk[i];
56+
}
57+
}
58+
59+
if (id) {
60+
headers['x-{{spec.title | caseLower }}-id'] = id;
61+
}
62+
63+
payload['{{ parameter.name }}'] = { type: 'file', file: new File([uploadableChunkTrimmed], {{ parameter.name | caseCamel | escapeKeyword }}.filename), filename: {{ parameter.name | caseCamel | escapeKeyword }}.filename };
64+
65+
response = await this.client.call('{{ method.method | caseLower }}', path, headers, payload{% if method.type == '___location' %}, 'arraybuffer'{% endif %});
66+
67+
if (!id) {
68+
id = response['$id'];
69+
}
70+
71+
if (onProgress !== null) {
72+
onProgress({
73+
$id: response['$id'],
74+
progress: Math.min((currentChunk) * Client.CHUNK_SIZE, size) / size * 100,
75+
sizeUploaded: end+1,
76+
chunksTotal: response['chunksTotal'],
77+
chunksUploaded: response['chunksUploaded']
78+
});
79+
}
80+
81+
uploadableChunk = new Uint8Array(Client.CHUNK_SIZE);
82+
currentPosition = 0;
83+
currentChunk++;
84+
}
85+
86+
for await (const chunk of {{ parameter.name | caseCamel | escapeKeyword }}.stream) {
87+
let i = 0;
88+
for(const b of chunk) {
89+
uploadableChunk[currentPosition] = chunk[i];
90+
91+
if(currentPosition + 1 >= Client.CHUNK_SIZE) {
92+
await uploadChunk();
93+
currentPosition--;
94+
}
95+
96+
i++;
97+
currentPosition++;
98+
}
99+
}
100+
101+
await uploadChunk(true);
102+
103+
return response;
104+
{% endif %}
105+
{% endfor %}

templates/deno/src/services/service.ts.twig

Lines changed: 3 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -73,134 +73,11 @@ export class {{ service.name | caseUcfirst }} extends Service {
7373
{% endif %}
7474
{% endfor %}
7575
let path = '{{ method.path }}'{% for parameter in method.parameters.path %}.replace('{{ '{' }}{{ parameter.name }}{{ '}' }}', {{ parameter.name | caseCamel | escapeKeyword }}){% endfor %};
76-
let payload: Payload = {};
77-
78-
{% for parameter in method.parameters.query %}
79-
if (typeof {{ parameter.name | caseCamel | escapeKeyword }} !== 'undefined') {
80-
payload['{{ parameter.name }}'] = {{ parameter.name | caseCamel | escapeKeyword }}{% if method.consumes[0] == "multipart/form-data" and ( parameter.type != "string" and parameter.type != "array" and parameter.type != "file" ) %}.toString(){% endif %};
81-
}
82-
83-
{% endfor %}
84-
{% for parameter in method.parameters.body %}
85-
if (typeof {{ parameter.name | caseCamel | escapeKeyword }} !== 'undefined') {
86-
payload['{{ parameter.name }}'] = {{ parameter.name | caseCamel | escapeKeyword }}{% if method.consumes[0] == "multipart/form-data" and ( parameter.type != "string" and parameter.type != "array" and parameter.type != "file" ) %}.toString(){% endif %};
87-
}
88-
{% endfor %}
76+
{{include('deno/base/params.twig')}}
8977
{% if 'multipart/form-data' in method.consumes %}
90-
{% for parameter in method.parameters.all %}
91-
{% if parameter.type == 'file' %}
92-
93-
const size = {{ parameter.name | caseCamel | escapeKeyword }}.size;
94-
95-
const headers: { [header: string]: string } = {
96-
{% for parameter in method.parameters.header %}
97-
'{{ parameter.name }}': ${{ parameter.name | caseCamel | escapeKeyword }},
98-
{% endfor %}
99-
{% for key, header in method.headers %}
100-
'{{ key }}': '{{ header }}',
101-
{% endfor %}
102-
};
103-
104-
let id: string | undefined = undefined;
105-
let response: any = undefined;
106-
107-
let chunksUploaded = 0;
108-
109-
{% for parameter in method.parameters.all %}
110-
{% if parameter.isUploadID %}
111-
if({{ parameter.name | caseCamel | escapeKeyword }} != 'unique()') {
112-
try {
113-
response = await this.client.call('get', path + '/' + {{ parameter.name }}, headers);
114-
chunksUploaded = response.chunksUploaded;
115-
} catch(e) {
116-
}
117-
}
118-
{% endif %}
119-
{% endfor %}
120-
121-
let currentChunk = 1;
122-
let currentPosition = 0;
123-
let uploadableChunk = new Uint8Array(Client.CHUNK_SIZE);
124-
125-
const uploadChunk = async (lastUpload = false) => {
126-
if(currentChunk <= chunksUploaded) {
127-
return;
128-
}
129-
130-
const start = ((currentChunk - 1) * Client.CHUNK_SIZE);
131-
const end = start + currentPosition;
132-
133-
if(!lastUpload || currentChunk !== 1) {
134-
headers['content-range'] = 'bytes ' + start + '-' + end + '/' + size;
135-
}
136-
137-
let uploadableChunkTrimmed: Uint8Array;
138-
139-
if(currentPosition + 1 >= Client.CHUNK_SIZE) {
140-
uploadableChunkTrimmed = uploadableChunk;
141-
} else {
142-
uploadableChunkTrimmed = new Uint8Array(currentPosition);
143-
for(let i = 0; i <= currentPosition; i++) {
144-
uploadableChunkTrimmed[i] = uploadableChunk[i];
145-
}
146-
}
147-
148-
if (id) {
149-
headers['x-{{spec.title | caseLower }}-id'] = id;
150-
}
151-
152-
payload['{{ parameter.name }}'] = { type: 'file', file: new File([uploadableChunkTrimmed], {{ parameter.name | caseCamel | escapeKeyword }}.filename), filename: {{ parameter.name | caseCamel | escapeKeyword }}.filename };
153-
154-
response = await this.client.call('{{ method.method | caseLower }}', path, headers, payload{% if method.type == '___location' %}, 'arraybuffer'{% endif %});
155-
156-
if (!id) {
157-
id = response['$id'];
158-
}
159-
160-
if (onProgress !== null) {
161-
onProgress({
162-
$id: response['$id'],
163-
progress: Math.min((currentChunk) * Client.CHUNK_SIZE, size) / size * 100,
164-
sizeUploaded: end+1,
165-
chunksTotal: response['chunksTotal'],
166-
chunksUploaded: response['chunksUploaded']
167-
});
168-
}
169-
170-
uploadableChunk = new Uint8Array(Client.CHUNK_SIZE);
171-
currentPosition = 0;
172-
currentChunk++;
173-
}
174-
175-
for await (const chunk of {{ parameter.name | caseCamel | escapeKeyword }}.stream) {
176-
let i = 0;
177-
for(const b of chunk) {
178-
uploadableChunk[currentPosition] = chunk[i];
179-
180-
if(currentPosition + 1 >= Client.CHUNK_SIZE) {
181-
await uploadChunk();
182-
currentPosition--;
183-
}
184-
185-
i++;
186-
currentPosition++;
187-
}
188-
}
189-
190-
await uploadChunk(true);
191-
192-
return response;
193-
{% endif %}
194-
{% endfor %}
78+
{{include('deno/base/requests/file.twig')}}
19579
{% else %}
196-
return await this.client.call('{{ method.method | caseLower }}', path, {
197-
{% for parameter in method.parameters.header %}
198-
'{{ parameter.name }}': ${{ parameter.name | caseCamel | escapeKeyword }},
199-
{% endfor %}
200-
{% for key, header in method.headers %}
201-
'{{ key }}': '{{ header }}',
202-
{% endfor %}
203-
}, payload);
80+
{{include('deno/base/requests/api.twig')}}
20481
{% endif %}
20582
}
20683
{% endfor %}

0 commit comments

Comments
 (0)