2
2
3
3
namespace Appwrite \SDK \Language ;
4
4
5
- class Node extends JS
5
+ class Node extends Web
6
6
{
7
7
/**
8
8
* @return string
@@ -12,31 +12,138 @@ public function getName(): string
12
12
return 'NodeJS ' ;
13
13
}
14
14
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
21
16
{
22
17
if (isset ($ parameter ['enumName ' ])) {
23
18
return \ucfirst ($ parameter ['enumName ' ]);
24
19
}
25
20
if (!empty ($ parameter ['enumValues ' ])) {
26
21
return \ucfirst ($ parameter ['name ' ]);
27
22
}
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<{}> ' ;
93
+ }
94
+
95
+ /**
96
+ * @param array $param
97
+ * @return string
98
+ */
99
+ public function getParamExample (array $ param ): string
100
+ {
101
+ $ type = $ param ['type ' ] ?? '' ;
102
+ $ example = $ param ['example ' ] ?? '' ;
103
+
104
+ $ output = '' ;
105
+
106
+ if (empty ($ example ) && $ example !== 0 && $ example !== false ) {
107
+ switch ($ type ) {
108
+ case self ::TYPE_NUMBER :
109
+ case self ::TYPE_INTEGER :
110
+ case self ::TYPE_BOOLEAN :
111
+ $ output .= 'null ' ;
112
+ break ;
113
+ case self ::TYPE_STRING :
114
+ $ output .= "'' " ;
115
+ break ;
116
+ case self ::TYPE_ARRAY :
117
+ $ output .= '[] ' ;
118
+ break ;
119
+ case self ::TYPE_OBJECT :
120
+ $ output .= '{} ' ;
121
+ break ;
122
+ case self ::TYPE_FILE :
123
+ $ output .= "InputFile.fromPath('/path/to/file', 'filename') " ;
124
+ break ;
125
+ }
126
+ } else {
127
+ switch ($ type ) {
128
+ case self ::TYPE_NUMBER :
129
+ case self ::TYPE_INTEGER :
130
+ case self ::TYPE_ARRAY :
131
+ case self ::TYPE_OBJECT :
132
+ $ output .= $ example ;
133
+ break ;
134
+ case self ::TYPE_BOOLEAN :
135
+ $ output .= ($ example ) ? 'true ' : 'false ' ;
136
+ break ;
137
+ case self ::TYPE_STRING :
138
+ $ output .= "' {$ example }' " ;
139
+ break ;
140
+ case self ::TYPE_FILE :
141
+ $ output .= "InputFile.fromPath('/path/to/file', 'filename') " ;
142
+ break ;
143
+ }
144
+ }
145
+
146
+ return $ output ;
40
147
}
41
148
42
149
/**
@@ -47,53 +154,48 @@ public function getFiles(): array
47
154
return [
48
155
[
49
156
'scope ' => 'default ' ,
50
- 'destination ' => 'index.js ' ,
51
- 'template ' => 'node/index.js .twig ' ,
157
+ 'destination ' => 'src/ index.ts ' ,
158
+ 'template ' => 'node/src/ index.ts .twig ' ,
52
159
],
53
160
[
54
161
'scope ' => 'default ' ,
55
- 'destination ' => 'index.d .ts ' ,
56
- 'template ' => 'node/index.d .ts.twig ' ,
162
+ 'destination ' => 'src/client .ts ' ,
163
+ 'template ' => 'node/src/client .ts.twig ' ,
57
164
],
58
165
[
59
166
'scope ' => 'default ' ,
60
- 'destination ' => 'lib/client.js ' ,
61
- 'template ' => 'node/lib/client.js .twig ' ,
167
+ 'destination ' => 'src/inputFile.ts ' ,
168
+ 'template ' => 'node/src/inputFile.ts .twig ' ,
62
169
],
63
170
[
64
- 'scope ' => 'default ' ,
65
- 'destination ' => 'lib/permission.js ' ,
66
- 'template ' => 'node/lib/permission.js .twig ' ,
171
+ 'scope ' => 'service ' ,
172
+ 'destination ' => 'src/services/{{service.name | caseDash}}.ts ' ,
173
+ 'template ' => 'node/src/services/template.ts .twig ' ,
67
174
],
68
175
[
69
176
'scope ' => 'default ' ,
70
- 'destination ' => 'lib/role.js ' ,
71
- 'template ' => 'node/lib/role.js .twig ' ,
177
+ 'destination ' => 'src/models.ts ' ,
178
+ 'template ' => 'web/src/models.ts .twig ' ,
72
179
],
73
180
[
74
181
'scope ' => 'default ' ,
75
- 'destination ' => 'lib/id.js ' ,
76
- 'template ' => 'node/lib/id.js .twig ' ,
182
+ 'destination ' => 'src/permission.ts ' ,
183
+ 'template ' => 'web/src/permission.ts .twig ' ,
77
184
],
78
185
[
79
186
'scope ' => 'default ' ,
80
- 'destination ' => 'lib/query.js ' ,
81
- 'template ' => 'node/lib/query.js .twig ' ,
187
+ 'destination ' => 'src/role.ts ' ,
188
+ 'template ' => 'web/src/role.ts .twig ' ,
82
189
],
83
190
[
84
191
'scope ' => 'default ' ,
85
- 'destination ' => 'lib/inputFile.js ' ,
86
- 'template ' => 'node/lib/inputFile.js .twig ' ,
192
+ 'destination ' => 'src/id.ts ' ,
193
+ 'template ' => 'web/src/id.ts .twig ' ,
87
194
],
88
195
[
89
196
'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 ' ,
197
+ 'destination ' => 'src/query.ts ' ,
198
+ 'template ' => 'web/src/query.ts.twig ' ,
97
199
],
98
200
[
99
201
'scope ' => 'default ' ,
@@ -115,80 +217,31 @@ public function getFiles(): array
115
217
'destination ' => 'package.json ' ,
116
218
'template ' => 'node/package.json.twig ' ,
117
219
],
118
- [
119
- 'scope ' => 'default ' ,
120
- 'destination ' => 'lib/exception.js ' ,
121
- 'template ' => 'node/lib/exception.js.twig ' ,
122
- ],
123
220
[
124
221
'scope ' => 'method ' ,
125
222
'destination ' => 'docs/examples/{{service.name | caseLower}}/{{method.name | caseDash}}.md ' ,
126
223
'template ' => 'node/docs/example.md.twig ' ,
127
224
],
225
+ [
226
+ 'scope ' => 'default ' ,
227
+ 'destination ' => 'tsconfig.json ' ,
228
+ 'template ' => '/node/tsconfig.json.twig ' ,
229
+ ],
230
+ [
231
+ 'scope ' => 'default ' ,
232
+ 'destination ' => 'tsup.config.js ' ,
233
+ 'template ' => 'node/tsup.config.js.twig ' ,
234
+ ],
128
235
[
129
236
'scope ' => 'default ' ,
130
237
'destination ' => '.travis.yml ' ,
131
238
'template ' => 'node/.travis.yml.twig ' ,
132
239
],
133
240
[
134
241
'scope ' => 'enum ' ,
135
- 'destination ' => 'lib /enums/{{ enum.name | caseDash }}.js ' ,
136
- 'template ' => 'node/lib /enums/enum.js .twig ' ,
242
+ 'destination ' => 'src /enums/{{ enum.name | caseDash }}.ts ' ,
243
+ 'template ' => 'web/src /enums/enum.ts .twig ' ,
137
244
],
138
245
];
139
246
}
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
- }
194
247
}
0 commit comments