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,84 @@ 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<{}> ' ;
40
93
}
41
94
42
95
/**
@@ -47,53 +100,48 @@ public function getFiles(): array
47
100
return [
48
101
[
49
102
'scope ' => 'default ' ,
50
- 'destination ' => 'index.js ' ,
51
- 'template ' => 'node/index.js .twig ' ,
103
+ 'destination ' => 'src/ index.ts ' ,
104
+ 'template ' => 'node/src/ index.ts .twig ' ,
52
105
],
53
106
[
54
107
'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 ' ,
57
110
],
58
111
[
59
112
'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 ' ,
62
115
],
63
116
[
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 ' ,
67
120
],
68
121
[
69
122
'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 ' ,
72
125
],
73
126
[
74
127
'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 ' ,
77
130
],
78
131
[
79
132
'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 ' ,
82
135
],
83
136
[
84
137
'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 ' ,
87
140
],
88
141
[
89
142
'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 ' ,
97
145
],
98
146
[
99
147
'scope ' => 'default ' ,
@@ -115,80 +163,31 @@ public function getFiles(): array
115
163
'destination ' => 'package.json ' ,
116
164
'template ' => 'node/package.json.twig ' ,
117
165
],
118
- [
119
- 'scope ' => 'default ' ,
120
- 'destination ' => 'lib/exception.js ' ,
121
- 'template ' => 'node/lib/exception.js.twig ' ,
122
- ],
123
166
[
124
167
'scope ' => 'method ' ,
125
168
'destination ' => 'docs/examples/{{service.name | caseLower}}/{{method.name | caseDash}}.md ' ,
126
169
'template ' => 'node/docs/example.md.twig ' ,
127
170
],
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
+ ],
128
181
[
129
182
'scope ' => 'default ' ,
130
183
'destination ' => '.travis.yml ' ,
131
184
'template ' => 'node/.travis.yml.twig ' ,
132
185
],
133
186
[
134
187
'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 ' ,
137
190
],
138
191
];
139
192
}
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
193
}
0 commit comments