Skip to content

Commit d875e74

Browse files
Merge pull request #799 from appwrite/feat-node-sdk-typescript
feat: node-appwrite use ts
2 parents b4c6327 + 5e9c5d4 commit d875e74

27 files changed

+642
-1180
lines changed

src/SDK/Language/Node.php

Lines changed: 103 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Appwrite\SDK\Language;
44

5-
class Node extends JS
5+
class Node extends Web
66
{
77
/**
88
* @return string
@@ -12,31 +12,84 @@ public function getName(): string
1212
return 'NodeJS';
1313
}
1414

15-
/**
16-
* @param array $parameter
17-
* @param array $spec
18-
* @return string
19-
*/
20-
public function getTypeName(array $parameter, array $spec = []): string
15+
public function getTypeName(array $parameter, array $method = []): string
2116
{
2217
if (isset($parameter['enumName'])) {
2318
return \ucfirst($parameter['enumName']);
2419
}
2520
if (!empty($parameter['enumValues'])) {
2621
return \ucfirst($parameter['name']);
2722
}
28-
return match ($parameter['type']) {
29-
self::TYPE_INTEGER,
30-
self::TYPE_NUMBER => 'number',
31-
self::TYPE_STRING => 'string',
32-
self::TYPE_FILE => 'InputFile',
33-
self::TYPE_BOOLEAN => 'boolean',
34-
self::TYPE_OBJECT => 'object',
35-
self::TYPE_ARRAY => (!empty(($parameter['array'] ?? [])['type']) && !\is_array($parameter['array']['type']))
36-
? $this->getTypeName($parameter['array']) . '[]'
37-
: 'string[]',
38-
default => $parameter['type'],
39-
};
23+
switch ($parameter['type']) {
24+
case self::TYPE_INTEGER:
25+
case self::TYPE_NUMBER:
26+
return 'number';
27+
case self::TYPE_ARRAY:
28+
if (!empty(($parameter['array'] ?? [])['type']) && !\is_array($parameter['array']['type'])) {
29+
return $this->getTypeName($parameter['array']) . '[]';
30+
}
31+
return 'string[]';
32+
case self::TYPE_FILE:
33+
return "File";
34+
case self::TYPE_OBJECT:
35+
if (empty($method)) {
36+
return $parameter['type'];
37+
}
38+
switch ($method['responseModel']) {
39+
case 'user':
40+
return "Partial<Preferences>";
41+
case 'document':
42+
if ($method['method'] === 'post') {
43+
return "Omit<Document, keyof Models.Document>";
44+
}
45+
if ($method['method'] === 'patch') {
46+
return "Partial<Omit<Document, keyof Models.Document>>";
47+
}
48+
}
49+
break;
50+
}
51+
return $parameter['type'];
52+
}
53+
54+
public function getReturn(array $method, array $spec): string
55+
{
56+
if ($method['type'] === 'webAuth') {
57+
return 'Promise<string>';
58+
}
59+
60+
if ($method['type'] === 'location') {
61+
return 'Promise<ArrayBuffer>';
62+
}
63+
64+
if (array_key_exists('responseModel', $method) && !empty($method['responseModel']) && $method['responseModel'] !== 'any') {
65+
$ret = 'Promise<';
66+
67+
if (
68+
array_key_exists($method['responseModel'], $spec['definitions']) &&
69+
array_key_exists('additionalProperties', $spec['definitions'][$method['responseModel']]) &&
70+
!$spec['definitions'][$method['responseModel']]['additionalProperties']
71+
) {
72+
$ret .= 'Models.';
73+
}
74+
75+
$ret .= $this->toPascalCase($method['responseModel']);
76+
77+
$models = [];
78+
79+
$this->populateGenerics($method['responseModel'], $spec, $models);
80+
81+
$models = array_unique($models);
82+
$models = array_filter($models, fn ($model) => $model != $this->toPascalCase($method['responseModel']));
83+
84+
if (!empty($models)) {
85+
$ret .= '<' . implode(', ', $models) . '>';
86+
}
87+
88+
$ret .= '>';
89+
90+
return $ret;
91+
}
92+
return 'Promise<{}>';
4093
}
4194

4295
/**
@@ -47,53 +100,48 @@ public function getFiles(): array
47100
return [
48101
[
49102
'scope' => 'default',
50-
'destination' => 'index.js',
51-
'template' => 'node/index.js.twig',
103+
'destination' => 'src/index.ts',
104+
'template' => 'node/src/index.ts.twig',
52105
],
53106
[
54107
'scope' => 'default',
55-
'destination' => 'index.d.ts',
56-
'template' => 'node/index.d.ts.twig',
108+
'destination' => 'src/client.ts',
109+
'template' => 'node/src/client.ts.twig',
57110
],
58111
[
59112
'scope' => 'default',
60-
'destination' => 'lib/client.js',
61-
'template' => 'node/lib/client.js.twig',
113+
'destination' => 'src/inputFile.ts',
114+
'template' => 'node/src/inputFile.ts.twig',
62115
],
63116
[
64-
'scope' => 'default',
65-
'destination' => 'lib/permission.js',
66-
'template' => 'node/lib/permission.js.twig',
117+
'scope' => 'service',
118+
'destination' => 'src/services/{{service.name | caseDash}}.ts',
119+
'template' => 'node/src/services/template.ts.twig',
67120
],
68121
[
69122
'scope' => 'default',
70-
'destination' => 'lib/role.js',
71-
'template' => 'node/lib/role.js.twig',
123+
'destination' => 'src/models.ts',
124+
'template' => 'web/src/models.ts.twig',
72125
],
73126
[
74127
'scope' => 'default',
75-
'destination' => 'lib/id.js',
76-
'template' => 'node/lib/id.js.twig',
128+
'destination' => 'src/permission.ts',
129+
'template' => 'web/src/permission.ts.twig',
77130
],
78131
[
79132
'scope' => 'default',
80-
'destination' => 'lib/query.js',
81-
'template' => 'node/lib/query.js.twig',
133+
'destination' => 'src/role.ts',
134+
'template' => 'web/src/role.ts.twig',
82135
],
83136
[
84137
'scope' => 'default',
85-
'destination' => 'lib/inputFile.js',
86-
'template' => 'node/lib/inputFile.js.twig',
138+
'destination' => 'src/id.ts',
139+
'template' => 'web/src/id.ts.twig',
87140
],
88141
[
89142
'scope' => 'default',
90-
'destination' => '/lib/service.js',
91-
'template' => 'node/lib/service.js.twig',
92-
],
93-
[
94-
'scope' => 'service',
95-
'destination' => '/lib/services/{{service.name | caseDash}}.js',
96-
'template' => 'node/lib/services/service.js.twig',
143+
'destination' => 'src/query.ts',
144+
'template' => 'web/src/query.ts.twig',
97145
],
98146
[
99147
'scope' => 'default',
@@ -115,80 +163,31 @@ public function getFiles(): array
115163
'destination' => 'package.json',
116164
'template' => 'node/package.json.twig',
117165
],
118-
[
119-
'scope' => 'default',
120-
'destination' => 'lib/exception.js',
121-
'template' => 'node/lib/exception.js.twig',
122-
],
123166
[
124167
'scope' => 'method',
125168
'destination' => 'docs/examples/{{service.name | caseLower}}/{{method.name | caseDash}}.md',
126169
'template' => 'node/docs/example.md.twig',
127170
],
171+
[
172+
'scope' => 'default',
173+
'destination' => 'tsconfig.json',
174+
'template' => '/node/tsconfig.json.twig',
175+
],
176+
[
177+
'scope' => 'default',
178+
'destination' => 'tsup.config.js',
179+
'template' => 'node/tsup.config.js.twig',
180+
],
128181
[
129182
'scope' => 'default',
130183
'destination' => '.travis.yml',
131184
'template' => 'node/.travis.yml.twig',
132185
],
133186
[
134187
'scope' => 'enum',
135-
'destination' => 'lib/enums/{{ enum.name | caseDash }}.js',
136-
'template' => 'node/lib/enums/enum.js.twig',
188+
'destination' => 'src/enums/{{ enum.name | caseDash }}.ts',
189+
'template' => 'web/src/enums/enum.ts.twig',
137190
],
138191
];
139192
}
140-
141-
/**
142-
* @param array $param
143-
* @return string
144-
*/
145-
public function getParamExample(array $param): string
146-
{
147-
$type = $param['type'] ?? '';
148-
$example = $param['example'] ?? '';
149-
150-
$output = '';
151-
152-
if (empty($example) && $example !== 0 && $example !== false) {
153-
switch ($type) {
154-
case self::TYPE_NUMBER:
155-
case self::TYPE_INTEGER:
156-
case self::TYPE_BOOLEAN:
157-
$output .= 'null';
158-
break;
159-
case self::TYPE_STRING:
160-
$output .= "''";
161-
break;
162-
case self::TYPE_ARRAY:
163-
$output .= '[]';
164-
break;
165-
case self::TYPE_OBJECT:
166-
$output .= '{}';
167-
break;
168-
case self::TYPE_FILE:
169-
$output .= "InputFile.fromPath('/path/to/file.png', 'file.png')";
170-
break;
171-
}
172-
} else {
173-
switch ($type) {
174-
case self::TYPE_NUMBER:
175-
case self::TYPE_INTEGER:
176-
case self::TYPE_ARRAY:
177-
case self::TYPE_OBJECT:
178-
$output .= $example;
179-
break;
180-
case self::TYPE_BOOLEAN:
181-
$output .= ($example) ? 'true' : 'false';
182-
break;
183-
case self::TYPE_STRING:
184-
$output .= "'{$example}'";
185-
break;
186-
case self::TYPE_FILE:
187-
$output .= "InputFile.fromPath('/path/to/file.png', 'file.png')";
188-
break;
189-
}
190-
}
191-
192-
return $output;
193-
}
194193
}

templates/node/base/params.twig

Lines changed: 0 additions & 23 deletions
This file was deleted.

templates/node/base/requests/api.twig

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)