diff --git a/example.php b/example.php index 6af298baf..62ab280f2 100644 --- a/example.php +++ b/example.php @@ -37,8 +37,8 @@ function getSSLPage($url) { } // Leave the platform you want uncommented - $platform = 'client'; - // $platform = 'console'; +// $platform = 'client'; + $platform = 'console'; // $platform = 'server'; $spec = getSSLPage("https://raw.githubusercontent.com/appwrite/appwrite/master/app/config/specs/swagger2-latest-{$platform}.json"); diff --git a/src/SDK/Language/Android.php b/src/SDK/Language/Android.php index 5d3e90bb0..4421bfa90 100644 --- a/src/SDK/Language/Android.php +++ b/src/SDK/Language/Android.php @@ -387,6 +387,11 @@ public function getFiles(): array 'destination' => 'library/src/main/java/io/appwrite/models/{{ definition.name | caseUcfirst }}.kt', 'template' => '/android/library/src/main/java/io/appwrite/models/Model.kt.twig', ], + [ + 'scope' => 'enum', + 'destination' => 'library/src/main/java/io/appwrite/enums/{{ enum.name | caseUcfirst }}.kt', + 'template' => '/android/library/src/main/java/io/appwrite/enums/Enum.kt.twig', + ], ]; } } diff --git a/src/SDK/Language/Apple.php b/src/SDK/Language/Apple.php index e3515de48..eb452ce1e 100644 --- a/src/SDK/Language/Apple.php +++ b/src/SDK/Language/Apple.php @@ -60,6 +60,11 @@ public function getFiles(): array 'destination' => '/Sources/{{ spec.title | caseUcfirst}}/WebSockets/WebSocketClientError.swift', 'template' => '/swift/Sources/WebSockets/WebSocketClientError.swift.twig', ], + [ + 'scope' => 'enum', + 'destination' => '/Sources/{{ spec.title | caseUcfirst}}/Enums/{{ enum.name | caseUcfirst }}.swift', + 'template' => '/swift/Sources/Enums/Enum.swift.twig', + ], // Config for project example-swiftui [ 'scope' => 'default', diff --git a/src/SDK/Language/Dart.php b/src/SDK/Language/Dart.php index d225b3768..753d6259f 100644 --- a/src/SDK/Language/Dart.php +++ b/src/SDK/Language/Dart.php @@ -466,6 +466,11 @@ public function getFiles(): array 'destination' => 'lib/src/input_file.dart', 'template' => 'dart/lib/src/input_file.dart.twig', ], + [ + 'scope' => 'enum', + 'destination' => 'lib/src/enums/{{ enum.name | caseSnake }}.dart', + 'template' => 'dart/lib/src/enums/enum.dart.twig', + ], ]; } diff --git a/src/SDK/Language/Deno.php b/src/SDK/Language/Deno.php index 1fdef0c0d..84327921b 100644 --- a/src/SDK/Language/Deno.php +++ b/src/SDK/Language/Deno.php @@ -93,6 +93,11 @@ public function getFiles(): array 'destination' => 'docs/examples/{{service.name | caseLower}}/{{method.name | caseDash}}.md', 'template' => 'deno/docs/example.md.twig', ], + [ + 'scope' => 'enum', + 'destination' => 'src/enums/{{ enum.name | caseDash }}.ts', + 'template' => 'deno/src/enums/enum.ts.twig', + ], ]; } @@ -102,6 +107,10 @@ public function getFiles(): array */ public function getTypeName(array $parameter): string { + if (isset($parameter['enumName'])) { + return $parameter['enumName']; + } + switch ($parameter['type']) { case self::TYPE_INTEGER: return 'number'; diff --git a/src/SDK/Language/DotNet.php b/src/SDK/Language/DotNet.php index 709f56ea5..ef8e19aad 100644 --- a/src/SDK/Language/DotNet.php +++ b/src/SDK/Language/DotNet.php @@ -129,7 +129,8 @@ public function getKeywords(): array 'when', 'where', 'while', - 'yield' + 'yield', + 'path' ]; } @@ -150,6 +151,7 @@ public function getIdentifierOverrides(): array */ public function getTypeName(array $parameter): string { + switch ($parameter['type']) { case self::TYPE_INTEGER: return 'long'; @@ -394,6 +396,11 @@ public function getFiles(): array 'scope' => 'definition', 'destination' => '/src/{{ spec.title | caseUcfirst }}/Models/{{ definition.name | caseUcfirst | overrideIdentifier }}.cs', 'template' => 'dotnet/src/Appwrite/Models/Model.cs.twig', + ], + [ + 'scope' => 'enum', + 'destination' => '/src/{{ spec.title | caseUcfirst }}/Enums/{{ enum.name | caseUcfirst | overrideIdentifier }}.cs', + 'template' => 'dotnet/src/Appwrite/Enums/Enums.cs.twig', ] ]; } diff --git a/src/SDK/Language/Flutter.php b/src/SDK/Language/Flutter.php index 806e19054..1da9709b5 100644 --- a/src/SDK/Language/Flutter.php +++ b/src/SDK/Language/Flutter.php @@ -330,6 +330,11 @@ public function getFiles(): array 'destination' => '.travis.yml', 'template' => 'flutter/.travis.yml.twig', ], + [ + 'scope' => 'enum', + 'destination' => 'lib/src/enums/{{ enum.name | caseSnake }}.dart', + 'template' => 'dart/lib/src/enums/enum.dart.twig', + ], ]; } } diff --git a/src/SDK/Language/JS.php b/src/SDK/Language/JS.php index 3ed496b24..a4a0ba64c 100644 --- a/src/SDK/Language/JS.php +++ b/src/SDK/Language/JS.php @@ -105,6 +105,7 @@ public function getKeywords(): array 'while', 'with', 'yield', + 'path' ]; } @@ -123,6 +124,7 @@ public function getIdentifierOverrides(): array */ public function getTypeName(array $parameter): string { + switch ($parameter['type']) { case self::TYPE_INTEGER: case self::TYPE_NUMBER: diff --git a/src/SDK/Language/Kotlin.php b/src/SDK/Language/Kotlin.php index f6a2af02b..df2329e21 100644 --- a/src/SDK/Language/Kotlin.php +++ b/src/SDK/Language/Kotlin.php @@ -86,7 +86,8 @@ public function getKeywords(): array "vararg", "when", "where", - "while" + "while", + "path" ]; } @@ -104,6 +105,7 @@ public function getIdentifierOverrides(): array */ public function getTypeName(array $parameter): string { + switch ($parameter['type']) { case self::TYPE_INTEGER: return 'Long'; @@ -418,6 +420,11 @@ public function getFiles(): array 'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/models/{{ definition.name | caseUcfirst }}.kt', 'template' => '/kotlin/src/main/kotlin/io/appwrite/models/Model.kt.twig', ], + [ + 'scope' => 'enum', + 'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/enums/{{ enum.name | caseUcfirst }}.kt', + 'template' => '/kotlin/src/main/kotlin/io/appwrite/enums/Enum.kt.twig', + ], ]; } diff --git a/src/SDK/Language/Node.php b/src/SDK/Language/Node.php index 435e1cce0..2f135e18e 100644 --- a/src/SDK/Language/Node.php +++ b/src/SDK/Language/Node.php @@ -126,6 +126,11 @@ public function getFiles(): array 'destination' => '.travis.yml', 'template' => 'node/.travis.yml.twig', ], + [ + 'scope' => 'enum', + 'destination' => 'lib/enums/{{ enum.name | caseDash }}.js', + 'template' => 'node/lib/enums/enum.js.twig', + ], ]; } diff --git a/src/SDK/Language/PHP.php b/src/SDK/Language/PHP.php index 3f6968814..c3f505e01 100644 --- a/src/SDK/Language/PHP.php +++ b/src/SDK/Language/PHP.php @@ -117,7 +117,8 @@ public function getKeywords(): array 'use', 'var', 'while', - 'xor' + 'xor', + 'path' ]; } @@ -211,6 +212,11 @@ public function getFiles(): array 'destination' => '/src/{{ spec.title | caseUcfirst}}/Services/{{service.name | caseUcfirst}}.php', 'template' => 'php/src/Services/Service.php.twig', ], + [ + 'scope' => 'enum', + 'destination' => '/src/{{ spec.title | caseUcfirst}}/Enums/{{ enum.name | caseUcfirst }}.php', + 'template' => 'php/src/Enums/Enum.php.twig', + ], ]; } @@ -221,6 +227,7 @@ public function getFiles(): array */ public function getTypeName(array $parameter): string { + switch ($parameter['type']) { case self::TYPE_STRING: $type = 'string'; diff --git a/src/SDK/Language/Python.php b/src/SDK/Language/Python.php index c8d19e639..e0901a60b 100644 --- a/src/SDK/Language/Python.php +++ b/src/SDK/Language/Python.php @@ -184,6 +184,11 @@ public function getFiles(): array 'destination' => '.travis.yml', 'template' => 'python/.travis.yml.twig', ], + [ + 'scope' => 'enum', + 'destination' => '{{ spec.title | caseSnake}}/enums/{{ enum.name | caseSnake }}.py', + 'template' => 'python/package/enums/enum.py.twig', + ], ]; } diff --git a/src/SDK/Language/Ruby.php b/src/SDK/Language/Ruby.php index d05021146..eb50551b0 100644 --- a/src/SDK/Language/Ruby.php +++ b/src/SDK/Language/Ruby.php @@ -74,6 +74,7 @@ public function getKeywords(): array 'until', 'when', 'while', + 'path' ]; } diff --git a/src/SDK/Language/Swift.php b/src/SDK/Language/Swift.php index 9b2e08798..ed08dc969 100644 --- a/src/SDK/Language/Swift.php +++ b/src/SDK/Language/Swift.php @@ -284,6 +284,11 @@ public function getFiles(): array 'destination' => '/Sources/{{ spec.title | caseUcfirst}}Models/{{ definition.name | caseUcfirst }}.swift', 'template' => '/swift/Sources/Models/Model.swift.twig', ], + [ + 'scope' => 'enum', + 'destination' => '/Sources/{{ spec.title | caseUcfirst}}Enums/{{ enum.name | caseUcfirst }}.swift', + 'template' => '/swift/Sources/Enums/Enum.swift.twig', + ] ]; } @@ -293,6 +298,10 @@ public function getFiles(): array */ public function getTypeName(array $parameter): string { + if (isset($parameter['enumName'])) { + return $parameter['enumName']; + } + switch ($parameter['type']) { case self::TYPE_INTEGER: return 'Int'; diff --git a/src/SDK/Language/Web.php b/src/SDK/Language/Web.php index 26ec51136..d6838af6c 100644 --- a/src/SDK/Language/Web.php +++ b/src/SDK/Language/Web.php @@ -115,6 +115,11 @@ public function getFiles(): array 'destination' => '.travis.yml', 'template' => 'web/.travis.yml.twig', ], + [ + 'scope' => 'enum', + 'destination' => 'src/enums/{{ enum.name | caseDash }}.ts', + 'template' => 'web/src/enums/enum.ts.twig', + ], ]; } diff --git a/src/SDK/SDK.php b/src/SDK/SDK.php index 73a75ce39..639fa1247 100644 --- a/src/SDK/SDK.php +++ b/src/SDK/SDK.php @@ -556,6 +556,7 @@ public function generate(string $target): void 'params' => $this->language->getParams(), ], 'sdk' => $this->getParams(), + 'enums' => $this->spec->getEnumNames(), ]; foreach ($this->language->getFiles() as $file) { @@ -634,6 +635,27 @@ public function generate(string $target): void } } break; + case 'enum': + foreach ($this->spec->getServices() as $key => $service) { + $methods = $this->spec->getMethods($key); + + foreach ($methods as $method) { + $parameters = $method['parameters']['all']; + + foreach ($parameters as $parameter) { + // Check if the enum field is defined + if (isset($parameter['enumValues'])) { + $params['enum'] = [ + 'name' => $parameter['enumName'] ?? $parameter['name'], + 'enum' => $parameter['enumValues'], + 'keys' => $parameter['enumKeys'], + ]; + $this->render($template, $destination, $block, $params, $minify); + } + } + } + } + break; } } } diff --git a/src/Spec/Spec.php b/src/Spec/Spec.php index 6ffbbba53..cf22cfa18 100644 --- a/src/Spec/Spec.php +++ b/src/Spec/Spec.php @@ -164,4 +164,11 @@ public function setAttribute($key, $value, $type = self::SET_TYPE_ASSIGN) return $this; } + + /** + * Get EnumNames + * + * @return array + */ + abstract public function getEnumNames(); } diff --git a/src/Spec/Swagger2.php b/src/Spec/Swagger2.php index bf343ea83..db436ac93 100644 --- a/src/Spec/Swagger2.php +++ b/src/Spec/Swagger2.php @@ -221,6 +221,11 @@ public function getMethods($service) } $param['default'] = (is_array($param['default'])) ? json_encode($param['default']) : $param['default']; + if (isset($parameter['enum'])) { + $param['enumValues'] = $parameter['enum']; + $param['enumName'] = $parameter['x-enum-name']; + $param['enumKeys'] = $parameter['x-enum-keys']; + } switch ($parameter['in']) { case 'header': @@ -240,24 +245,31 @@ public function getMethods($service) $bodyRequired = $parameter['schema']['required'] ?? []; foreach ($bodyProperties as $key => $value) { - $param['name'] = $key; - $param['type'] = $value['type'] ?? null; - $param['description'] = $value['description'] ?? ''; - $param['required'] = (in_array($key, $bodyRequired)); - $param['example'] = $value['x-example'] ?? null; - $param['isUploadID'] = $value['x-upload-id'] ?? false; - $param['nullable'] = $value['x-nullable'] ?? false; - $param['array'] = [ + $temp = $param; + $temp['name'] = $key; + $temp['type'] = $value['type'] ?? null; + $temp['description'] = $value['description'] ?? ''; + $temp['required'] = (in_array($key, $bodyRequired)); + $temp['example'] = $value['x-example'] ?? null; + $temp['isUploadID'] = $value['x-upload-id'] ?? false; + $temp['nullable'] = $value['x-nullable'] ?? false; + $temp['array'] = [ 'type' => $value['items']['type'] ?? '', ]; if ($value['type'] === 'object' && is_array($value['default'])) { $value['default'] = (empty($value['default'])) ? new stdClass() : $value['default']; } - $param['default'] = (is_array($value['default']) || $value['default'] instanceof stdClass) ? json_encode($value['default']) : $value['default']; + if (isset($value['enum'])) { + $temp['enumValues'] = $value['enum']; + $temp['enumName'] = $value['x-enum-name']; + $temp['enumKeys'] = $value['x-enum-keys']; + } + + $temp['default'] = (is_array($value['default']) || $value['default'] instanceof stdClass) ? json_encode($value['default']) : $value['default']; - $output['parameters']['body'][] = $param; - $output['parameters']['all'][] = $param; + $output['parameters']['body'][] = $temp; + $output['parameters']['all'][] = $temp; } continue 2; @@ -341,4 +353,25 @@ public function getDefinitions() } return $list; } + + /** + * @return array + */ + public function getEnumNames(): array + { + $list = []; + + foreach ($this->getServices() as $key => $service) { + foreach ($this->getMethods($key) as $method) { + if (isset($method['parameters']) && is_array($method['parameters'])) { + foreach ($method['parameters']['all'] as $parameter) { + if (isset($parameter['enumValues'])) { + $list[] = $parameter['enumName'] ?? $parameter['name']; + } + } + } + } + } + return \array_values(\array_unique($list)); + } } diff --git a/templates/android/docs/java/example.md.twig b/templates/android/docs/java/example.md.twig index 23f797098..710cc6dcd 100644 --- a/templates/android/docs/java/example.md.twig +++ b/templates/android/docs/java/example.md.twig @@ -4,6 +4,10 @@ import {{ sdk.namespace | caseDot }}.coroutines.CoroutineCallback; import {{ sdk.namespace | caseDot }}.models.InputFile; {% endif %} import {{ sdk.namespace | caseDot }}.services.{{ service.name | caseUcfirst }}; +{% for parameter in method.parameters.all %}{% if parameter.required %}{% if not loop.first %}{% endif %} +{% if parameter.enumValues | length > 0%}import {{ sdk.namespace | caseDot }}.enums.{{parameter.enumName | caseUcfirst}};{% endif %} + +{% endif %}{% endfor %} Client client = new Client(context) {% if method.auth|length > 0 %} @@ -26,7 +30,7 @@ Client client = new Client(context) {% for parameter in method.parameters.all %} {% if parameter.required %} - {{ parameter | paramExample }}{% if not loop.last %},{% endif %} + {% if parameter.enumValues | length > 0%}{{parameter.enumName}}.{{(parameter.enumKeys[0] ?? parameter.enumValues[0]) | caseUpper | replace({'-': '',' ':''}) }}{% else%}{{ parameter | paramExample }}{% endif %}{% if not loop.last %},{% endif %} {% endif %} {% if loop.last %} diff --git a/templates/android/docs/kotlin/example.md.twig b/templates/android/docs/kotlin/example.md.twig index 8acb6e64a..2b0c44439 100644 --- a/templates/android/docs/kotlin/example.md.twig +++ b/templates/android/docs/kotlin/example.md.twig @@ -3,6 +3,10 @@ import {{ sdk.namespace | caseDot }}.Client import {{ sdk.namespace | caseDot }}.models.InputFile {% endif %} import {{ sdk.namespace | caseDot }}.services.{{ service.name | caseUcfirst }} +{% for parameter in method.parameters.all %}{% if parameter.required %}{% if not loop.first %}{% endif %} +{% if parameter.enumValues | length > 0%}import {{ sdk.namespace | caseDot }}.enums.{{parameter.enumName | caseUcfirst}}{% endif %} + +{% endif %}{% endfor %} val client = Client(context) {% if method.auth|length > 0 %} @@ -18,7 +22,7 @@ val {{ service.name | caseCamel }} = {{ service.name | caseUcfirst }}(client) {% for parameter in method.parameters.all %} {% if parameter.required %} - {{parameter.name}} = {{ parameter | paramExample }}{% if not loop.last %},{% endif %} + {{parameter.name}} = {% if parameter.enumValues | length > 0%}{{parameter.enumName}}.{{(parameter.enumKeys[0] ?? parameter.enumValues[0]) | caseUcfirst | replace({'-': '',' ':''}) }}{% else%}{{ parameter | paramExample }}{% endif %}{% if not loop.last %},{% endif %} {% endif %} {% if loop.last %} diff --git a/templates/android/library/src/main/java/io/appwrite/enums/Enum.kt.twig b/templates/android/library/src/main/java/io/appwrite/enums/Enum.kt.twig new file mode 100644 index 000000000..71959357e --- /dev/null +++ b/templates/android/library/src/main/java/io/appwrite/enums/Enum.kt.twig @@ -0,0 +1,10 @@ +package {{ sdk.namespace | caseDot }}.enums + +enum class {{ enum.name | caseUcfirst | overrideIdentifier }}(val value: String) { +{% for value in enum.enum %} + {% set key = enum.keys is empty ? value : enum.keys[loop.index0] %} + {{ key | caseUpper | replace({'-': '',' ':''})}}("{{value}}"){% if not loop.last %}, +{% endif %} +{% endfor %} + +} \ No newline at end of file diff --git a/templates/cli/lib/config.js.twig b/templates/cli/lib/config.js.twig index 744013e38..916865f44 100644 --- a/templates/cli/lib/config.js.twig +++ b/templates/cli/lib/config.js.twig @@ -15,7 +15,6 @@ class Config { const file = fs.readFileSync(this.path).toString(); this.data = JSONbig.parse(file); } catch (e) { - // console.log(`${this.path} not found. Empty data`); this.data = {}; } } @@ -25,7 +24,7 @@ class Config { if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }); } - fs.writeFileSync(this.path, JSONbig.stringify(this.data, null, 4)); + fs.writeFileSync(this.path, JSONbig.stringify(this.data, null, 4), { mode: 0o600 }); } get(key) { diff --git a/templates/dart/docs/example.md.twig b/templates/dart/docs/example.md.twig index 5e2afae03..b0f129138 100644 --- a/templates/dart/docs/example.md.twig +++ b/templates/dart/docs/example.md.twig @@ -22,7 +22,7 @@ void main() { // Init SDK {% endif %} {% if parameter.required %} - {{ parameter.name | caseCamel | overrideIdentifier }}: {{ parameter | paramExample | replace({'': (parameter.name | caseCamel) }) | raw }}, + {{ parameter.name | caseCamel | overrideIdentifier }}:{% if parameter.enumValues | length > 0%} {{parameter.enumName}}.{{(parameter.enumKeys[0] ?? parameter.enumValues[0]) | caseCamel | replace({'-': ''}) }}.value{% else%}{{ parameter | paramExample | replace({'': (parameter.name | caseCamel) }) | raw }} {% endif %}, {% endif %} {% endfor %}{% if method.parameters.all | length > 0 %} {% endif %}); @@ -32,4 +32,4 @@ void main() { // Init SDK }).catchError((error) { print(error.response); }); -} \ No newline at end of file +}} \ No newline at end of file diff --git a/templates/dart/lib/package.dart.twig b/templates/dart/lib/package.dart.twig index 2ff4077c0..0b0942d8e 100644 --- a/templates/dart/lib/package.dart.twig +++ b/templates/dart/lib/package.dart.twig @@ -27,3 +27,6 @@ part 'id.dart'; {% for service in spec.services %} part 'services/{{service.name | caseDash}}.dart'; {% endfor %} +{% for enum in enums %} +part 'src/enums/{{enum | caseSnake}}.dart'; +{% endfor %} diff --git a/templates/dart/lib/src/enums/enum.dart.twig b/templates/dart/lib/src/enums/enum.dart.twig new file mode 100644 index 000000000..29c574c3e --- /dev/null +++ b/templates/dart/lib/src/enums/enum.dart.twig @@ -0,0 +1,13 @@ +part of {{ language.params.packageName }}; + +enum {{ enum.name | caseUcfirst | overrideIdentifier }} { + {% for value in enum.enum %} +{% set key = enum.keys is empty ? value : enum.keys[loop.index0] %} + {{ key | caseCamel | replace({'-': ''})}}('{{ value }}'){% if not loop.last %}, +{% endif %} +{% if loop.last %};{% endif %} + {% endfor %} + +final String value; +const {{ enum.name | caseUcfirst | overrideIdentifier }}(this.value); +} \ No newline at end of file diff --git a/templates/deno/docs/example.md.twig b/templates/deno/docs/example.md.twig index 55f3735ee..e78c1fbf6 100644 --- a/templates/deno/docs/example.md.twig +++ b/templates/deno/docs/example.md.twig @@ -19,7 +19,7 @@ client {% endif %} -let promise = {{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% for parameter in method.parameters.all %}{% if parameter.required %}{% if not loop.first %}, {% endif %}{{ parameter | paramExample }}{% endif %}{% endfor %}); +let promise = {{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% for parameter in method.parameters.all %}{% if parameter.required %}{% if not loop.first %}, {% endif %}{% if parameter.enumValues | length > 0%}{{parameter.enumName}}.{{(parameter.enumKeys[0] ?? parameter.enumValues[0]) | caseUcfirst | replace({'-': ''}) }}{% else%}{{ parameter | paramExample }}{% endif %}{% endif %}{% endfor %}); promise.then(function (response) { console.log(response); diff --git a/templates/deno/mod.ts.twig b/templates/deno/mod.ts.twig index 2878b6360..4d6e5bab7 100644 --- a/templates/deno/mod.ts.twig +++ b/templates/deno/mod.ts.twig @@ -8,6 +8,9 @@ import { {{spec.title | caseUcfirst}}Exception } from "./src/exception.ts"; {% for service in spec.services %} import { {{service.name | caseUcfirst}} } from "./src/services/{{service.name | caseDash}}.ts"; {% endfor %} +{% for enum in enums %} +import { {{enum | caseUcfirst}} } from "./src/enums/{{enum | caseDash}}.ts"; +{% endfor %} export { Client, @@ -20,6 +23,9 @@ export { {% for service in spec.services %} {{service.name | caseUcfirst}}, {% endfor %} +{% for enum in enums %} + {{enum | caseUcfirst}}, +{% endfor %} }; export type { Models } from "./src/models.d.ts"; diff --git a/templates/deno/src/enums/enum.ts.twig b/templates/deno/src/enums/enum.ts.twig new file mode 100644 index 000000000..b94dd355f --- /dev/null +++ b/templates/deno/src/enums/enum.ts.twig @@ -0,0 +1,6 @@ +export enum {{ enum.name | caseUcfirst | overrideIdentifier }} { + {% for value in enum.enum %} + {% set key = enum.keys is empty ? value : enum.keys[loop.index0] %} + {{ key | caseUcfirst | replace({'-': ''}) }} = '{{ value }}', +{% endfor %} +} \ No newline at end of file diff --git a/templates/dotnet/docs/example.md.twig b/templates/dotnet/docs/example.md.twig index 2ce25f701..d6c1208e4 100644 --- a/templates/dotnet/docs/example.md.twig +++ b/templates/dotnet/docs/example.md.twig @@ -1,6 +1,9 @@ using {{ spec.title | caseUcfirst }}; using {{ spec.title | caseUcfirst }}.Services; using {{ spec.title | caseUcfirst }}.Models; +{% for parameter in method.parameters.all %}{% if parameter.required %}{% if not loop.first %}{% endif %} +using {{ spec.title | caseUcfirst }}.Enums; +{% endif %}{% endfor %} var client = new Client() {% if method.auth|length > 0 %} @@ -15,4 +18,4 @@ var {{ service.name | caseCamel }} = new {{ service.name | caseUcfirst }}(client {% if method.method != 'delete' %}{% if method.type == 'location' %}byte[]{% else %}{{ method.responseModel | caseUcfirst | overrideIdentifier }}{% endif %} result = {% endif %}await {{ service.name | caseCamel }}.{{ method.name | caseUcfirst }}({% if method.parameters.all | length == 0 %});{% endif %} {% for parameter in method.parameters.all %}{% if parameter.required %}{% if not loop.first %},{% endif %} - {{ parameter.name }}: {{ parameter | paramExample }}{% endif %}{% endfor %}{% if method.parameters.all | length > 0 %});{% endif %} + {{ parameter.name }}: {% if parameter.enumValues | length > 0%} {{parameter.enumName}}.{{(parameter.enumKeys[0] ?? parameter.enumValues[0]) | caseUcfirst | replace({'-': ''}) }}{% else%}{{ parameter | paramExample }}{% endif %}{% endif %}{% endfor %}{% if method.parameters.all | length > 0 %});{% endif %} diff --git a/templates/dotnet/src/Appwrite/Enums/Enums.cs.twig b/templates/dotnet/src/Appwrite/Enums/Enums.cs.twig new file mode 100644 index 000000000..2d04bb3a6 --- /dev/null +++ b/templates/dotnet/src/Appwrite/Enums/Enums.cs.twig @@ -0,0 +1,12 @@ +using System; + +namespace {{ spec.title | caseUcfirst }}.Enums +{ + public static class {{ enum.name | caseUcfirst | overrideIdentifier }} + { + {% for value in enum.enum %} + {% set key = enum.keys is empty ? value : enum.keys[loop.index0] %} + public static string {{ key | caseUcfirst | replace({'-': ''}) }} => "{{ value }}"; + {% endfor %} + } +} diff --git a/templates/flutter/docs/example.md.twig b/templates/flutter/docs/example.md.twig index db87c778c..fe0c3d629 100644 --- a/templates/flutter/docs/example.md.twig +++ b/templates/flutter/docs/example.md.twig @@ -23,7 +23,7 @@ void main() { // Init SDK {% endif %} {% if parameter.required %} - {{ parameter.name | caseCamel }}: {{ parameter | paramExample }}, + {{ parameter.name | caseCamel | overrideIdentifier}}:{% if parameter.enumValues | length > 0%} {{parameter.enumName}}.{{(parameter.enumKeys[0] ?? parameter.enumValues[0]) | caseCamel | replace({'-': ''}) }}.value{% else %}{{ parameter | paramExample}} {% endif %}, {% endif %} {% endfor %}{% if method.parameters.all | length > 0 %} {% endif %}).then((bytes) { final file = File('path_to_file/filename.ext'); @@ -40,7 +40,7 @@ FutureBuilder( {% endif %} {% if parameter.required %} - {{ parameter.name | caseCamel | overrideIdentifier }}: {{ parameter | paramExample }}, + {{ parameter.name | caseCamel | overrideIdentifier}}:{% if parameter.enumValues | length > 0%} {{parameter.enumName}}.{{(parameter.enumKeys[0] ?? parameter.enumValues[0]) | caseCamel | replace({'-': ''}) }}.value{% else %}{{ parameter | paramExample}} {% endif %}, {% endif %} {% endfor %}{% if method.parameters.all | length > 0 %} {% endif %} ), //works for both public file and private file, for private files you need to be logged in @@ -58,7 +58,7 @@ FutureBuilder( {% endif %} {% if parameter.required %} - {{ parameter.name | caseCamel }}: {{ parameter | paramExample }}, + {{ parameter.name | caseCamel | overrideIdentifier}}:{% if parameter.enumValues | length > 0%} {{parameter.enumName}}.{{(parameter.enumKeys[0] ?? parameter.enumValues[0]) | caseCamel | replace({'-': ''}) }}.value{% else %}{{ parameter | paramExample}} {% endif %}, {% endif %} {% endfor %}{% if method.parameters.all | length > 0 %} {% endif %}); diff --git a/templates/flutter/lib/package.dart.twig b/templates/flutter/lib/package.dart.twig index 9d92197b1..ca6dc09c8 100644 --- a/templates/flutter/lib/package.dart.twig +++ b/templates/flutter/lib/package.dart.twig @@ -28,4 +28,7 @@ part 'role.dart'; part 'id.dart'; {% for service in spec.services %} part 'services/{{service.name | caseDash}}.dart'; +{% endfor %} +{% for enum in enums %} +part 'src/enums/{{enum | caseSnake}}.dart'; {% endfor %} \ No newline at end of file diff --git a/templates/kotlin/docs/java/example.md.twig b/templates/kotlin/docs/java/example.md.twig index 75508a1ff..d597795bb 100644 --- a/templates/kotlin/docs/java/example.md.twig +++ b/templates/kotlin/docs/java/example.md.twig @@ -4,6 +4,10 @@ import {{ sdk.namespace | caseDot }}.coroutines.CoroutineCallback; import {{ sdk.namespace | caseDot }}.models.InputFile; {% endif %} import {{ sdk.namespace | caseDot }}.services.{{ service.name | caseUcfirst }}; +{% for parameter in method.parameters.all %}{% if parameter.required %}{% if not loop.first %}{% endif %} +{% if parameter.enumValues | length > 0%}import {{ sdk.namespace | caseDot }}.enums.{{parameter.enumName | caseUcfirst}};{% endif %} + +{% endif %}{% endfor %} Client client = new Client() {% if method.auth|length > 0 %} @@ -26,7 +30,7 @@ Client client = new Client() {% for parameter in method.parameters.all %} {% if parameter.required %} - {{ parameter | paramExample }}{% if not loop.last %},{% endif %} + {% if parameter.enumValues | length > 0%} {{parameter.enumName}}.{{(parameter.enumKeys[0] ?? parameter.enumValues[0]) | caseUpper | replace({'-': '',' ':''}) }}{% else%}{{ parameter | paramExample }}{% endif %}{% if not loop.last %},{% endif %} {% endif %} {% if loop.last %} diff --git a/templates/kotlin/docs/kotlin/example.md.twig b/templates/kotlin/docs/kotlin/example.md.twig index 8acb6e64a..16e8d525f 100644 --- a/templates/kotlin/docs/kotlin/example.md.twig +++ b/templates/kotlin/docs/kotlin/example.md.twig @@ -3,6 +3,10 @@ import {{ sdk.namespace | caseDot }}.Client import {{ sdk.namespace | caseDot }}.models.InputFile {% endif %} import {{ sdk.namespace | caseDot }}.services.{{ service.name | caseUcfirst }} +{% for parameter in method.parameters.all %}{% if parameter.required %}{% if not loop.first %}{% endif %} +{% if parameter.enumValues | length > 0%}import {{ sdk.namespace | caseDot }}.enums.{{parameter.enumName | caseUcfirst}}{% endif %} + +{% endif %}{% endfor %} val client = Client(context) {% if method.auth|length > 0 %} @@ -18,7 +22,7 @@ val {{ service.name | caseCamel }} = {{ service.name | caseUcfirst }}(client) {% for parameter in method.parameters.all %} {% if parameter.required %} - {{parameter.name}} = {{ parameter | paramExample }}{% if not loop.last %},{% endif %} + {{parameter.name}} = {% if parameter.enumValues | length > 0%} {{parameter.enumName}}.{{(parameter.enumKeys[0] ?? parameter.enumValues[0]) | caseUpper | replace({'-': '',' ': '_'}) }}{% else%}{{ parameter | paramExample }}{% endif %}{% if not loop.last %},{% endif %} {% endif %} {% if loop.last %} diff --git a/templates/kotlin/src/main/kotlin/io/appwrite/enums/Enum.kt.twig b/templates/kotlin/src/main/kotlin/io/appwrite/enums/Enum.kt.twig new file mode 100644 index 000000000..1322953de --- /dev/null +++ b/templates/kotlin/src/main/kotlin/io/appwrite/enums/Enum.kt.twig @@ -0,0 +1,10 @@ +package {{ sdk.namespace | caseDot }}.enums + +enum class {{ enum.name | caseUcfirst | overrideIdentifier }}(val value: String) { +{% for value in enum.enum %} + {% set key = enum.keys is empty ? value : enum.keys[loop.index0] %} + {{ key | caseUpper | replace({'-': '',' ': ''})}}("{{value}}"){% if not loop.last %}, +{% endif %} +{% endfor %} + +} \ No newline at end of file diff --git a/templates/node/docs/example.md.twig b/templates/node/docs/example.md.twig index 9c0a7a260..db06dbd04 100644 --- a/templates/node/docs/example.md.twig +++ b/templates/node/docs/example.md.twig @@ -18,7 +18,7 @@ client {% endfor %}; {% endif %} -const promise = {{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% for parameter in method.parameters.all %}{% if parameter.required %}{% if not loop.first %}, {% endif %}{{ parameter | paramExample }}{% endif %}{% endfor %}); +const promise = {{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% for parameter in method.parameters.all %}{% if parameter.required %}{% if not loop.first %}, {% endif %}{% if parameter.enumValues | length > 0%}{{parameter.enumName}}.{{(parameter.enumKeys[0] ?? parameter.enumValues[0]) | replace({'-': '',' ': ''}) }}{% else%}{{ parameter | paramExample}}{% endif %}{% endif %}{% endfor %}); promise.then(function (response) { console.log(response); diff --git a/templates/node/index.js.twig b/templates/node/index.js.twig index b82be2323..ddd31be72 100644 --- a/templates/node/index.js.twig +++ b/templates/node/index.js.twig @@ -8,6 +8,9 @@ const {{spec.title | caseUcfirst}}Exception = require('./lib/exception.js'); {% for service in spec.services %} const {{service.name | caseUcfirst}} = require('./lib/services/{{service.name | caseDash}}.js'); {% endfor %} +{% for enum in enums %} +const {{enum | caseUcfirst}} = require("./lib/enums/{{enum | caseDash}}.js"); +{% endfor %} module.exports = { Client, @@ -19,5 +22,7 @@ module.exports = { {{spec.title | caseUcfirst}}Exception, {% for service in spec.services %} {{service.name | caseUcfirst}}, +{% endfor %}{% for enum in enums %} + {{ enum | caseUcfirst }}, {% endfor %} }; diff --git a/templates/node/lib/enums/enum.js.twig b/templates/node/lib/enums/enum.js.twig new file mode 100644 index 000000000..e9afe841d --- /dev/null +++ b/templates/node/lib/enums/enum.js.twig @@ -0,0 +1,9 @@ +const {{ enum.name | caseUcfirst | overrideIdentifier }} = { + {% for value in enum.enum %} + {% set key = enum.keys is empty ? value : enum.keys[loop.index0] %} + {{ key | caseLower | replace({'-': '',' ': ''})}}: '{{value}}' {% if not loop.last %}, + {% endif %} + {% endfor %} +} + +module.exports = {{ enum.name | caseUcfirst | overrideIdentifier }}; \ No newline at end of file diff --git a/templates/php/docs/example.md.twig b/templates/php/docs/example.md.twig index 8936a48d3..ab296d8da 100644 --- a/templates/php/docs/example.md.twig +++ b/templates/php/docs/example.md.twig @@ -5,6 +5,10 @@ use {{ spec.title | caseUcfirst }}\Client; use {{ spec.title | caseUcfirst }}\InputFile; {% endif %} use {{ spec.title | caseUcfirst }}\Services\{{ service.name | caseUcfirst }}; +{% for parameter in method.parameters.all %}{% if parameter.required %}{% if not loop.first %}{% endif %} +{% if parameter.enumValues | length > 0%}use {{ spec.title | caseUcfirst }}\Enums\{{parameter.enumName | caseUcfirst}};{% endif %} + +{% endif %}{% endfor %} $client = new Client(); @@ -20,4 +24,4 @@ $client {% endif %} ${{ service.name | caseCamel }} = new {{ service.name | caseUcfirst }}($client{% if service.globalParams | length %}{% for parameter in service.globalParams %}, {{ parameter | paramExample }}{% endfor %}{% endif %}); -$result = ${{ service.name | caseCamel }}->{{ method.name | caseCamel }}({% for parameter in method.parameters.all %}{% if parameter.required %}{% if not loop.first %}, {% endif %}{{ parameter | paramExample }}{% endif %}{% endfor %}); \ No newline at end of file +$result = ${{ service.name | caseCamel }}->{{ method.name | caseCamel }}({% for parameter in method.parameters.all %}{% if parameter.required %}{% if not loop.first %}, {% endif %}{% if parameter.enumValues | length > 0%}{{parameter.enumName}}.{{(parameter.enumKeys[0] ?? parameter.enumValues[0]) | caseUpper | replace({'-': '',' ': '_'}) }}{% else%}{{ parameter | paramExample }}{% endif %}{% endif %}{% endfor %}); \ No newline at end of file diff --git a/templates/php/src/Enums/Enum.php.twig b/templates/php/src/Enums/Enum.php.twig new file mode 100644 index 000000000..66c0afd79 --- /dev/null +++ b/templates/php/src/Enums/Enum.php.twig @@ -0,0 +1,11 @@ + 0%}from {{ spec.title | caseUcfirst }}.enums import {{parameter.enumName | caseUcfirst}},{% endif %} +{% endif %}{% endfor %} client = Client() @@ -18,4 +21,4 @@ client = Client() {% endif %} {{ service.name | caseSnake }} = {{ service.name | caseUcfirst }}(client{% if service.globalParams | length %}{% for parameter in service.globalParams %}, {{ parameter | paramExample }}{% endfor %}{% endif %}) -result = {{ service.name | caseSnake }}.{{ method.name | caseSnake }}({% for parameter in method.parameters.all %}{% if parameter.required %}{% if not loop.first %}, {% endif %}{{ parameter | paramExample }}{% endif %}{% endfor %}) +result = {{ service.name | caseSnake }}.{{ method.name | caseSnake }}({% for parameter in method.parameters.all %}{% if parameter.required %}{% if not loop.first %}, {% endif %}{% if parameter.enumValues | length > 0%} {{parameter.enumName}}.{{(parameter.enumKeys[0] ?? parameter.enumValues[0]) | caseUpper | replace({'-': '',' ': '_'}) }}{% else%}{{ parameter | paramExample }}{% endif %}{% endif %}{% endfor %}) diff --git a/templates/python/package/enums/enum.py.twig b/templates/python/package/enums/enum.py.twig new file mode 100644 index 000000000..0358d0549 --- /dev/null +++ b/templates/python/package/enums/enum.py.twig @@ -0,0 +1,7 @@ +from enum import Enum + +class {{ enum.name | caseUcfirst | overrideIdentifier }}(Enum): +{% for value in enum.enum %} + {% set key = enum.keys is empty ? value : enum.keys[loop.index0] %} + {{ key | caseUpper | replace({'-': '',' ': ''})}} = "{{ value }}" +{% endfor %} \ No newline at end of file diff --git a/templates/swift/Sources/Enums/Enum.swift.twig b/templates/swift/Sources/Enums/Enum.swift.twig new file mode 100644 index 000000000..341164ab4 --- /dev/null +++ b/templates/swift/Sources/Enums/Enum.swift.twig @@ -0,0 +1,5 @@ +enum {{ enum.name | caseUcfirst | overrideIdentifier }} : String { +{% for value in enum.enum %} + {% set key = enum.keys is empty ? value : enum.keys[loop.index0] %} + case {{ key | caseCamel | replace({'-': ''})}} = '{{ value }}' +{% endfor %} diff --git a/templates/swift/docs/example.md.twig b/templates/swift/docs/example.md.twig index 2d4f478db..31eca4603 100644 --- a/templates/swift/docs/example.md.twig +++ b/templates/swift/docs/example.md.twig @@ -1,4 +1,9 @@ import Appwrite +{% for parameter in method.parameters.all | filter((parameter) => parameter.required) %} +{% if parameter.enumValues | length > 0 %} +import AppwriteEnums +{% endif %} +{% endfor %} let client = Client() {% if method.auth|length > 0 %} @@ -15,7 +20,7 @@ let {{ service.name | caseCamel }} = {{ service.name | caseUcfirst }}(client{% i let {% if method.type == 'webAuth' %}success{% elseif method.type == 'location' %}byteBuffer{% elseif method.responseModel | length == 0 %}result{% else %}{{ method.responseModel | caseCamel | escapeKeyword }}{% endif %} = try await {{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% if method.parameters.all | filter((param) => param.required) | length == 0 %}){{ '\n' }}{% endif %} {% for parameter in method.parameters.all | filter((parameter) => parameter.required) %} - {{parameter.name}}: {{ parameter | paramExample | escapeKeyword }}{% if not loop.last %},{% endif %} + {{parameter.name}}: {% if parameter.enumValues | length > 0%}{{parameter.enumName}}.{{(parameter.enumKeys[0] ?? parameter.enumValues[0]) | caseUcfirst | replace({'-': ''}) }}{% else%}{{ parameter | paramExample | escapeKeyword }}{% endif %}{% if not loop.last %},{% endif %} {% if loop.last %} ) diff --git a/templates/web/docs/example.md.twig b/templates/web/docs/example.md.twig index 0d5862963..ff1bc6f2c 100644 --- a/templates/web/docs/example.md.twig +++ b/templates/web/docs/example.md.twig @@ -1,4 +1,4 @@ -import {{ '{' }} Client, {{service.name | caseUcfirst}} {{ '}' }} from "{{ language.params.npmPackage }}"; +import {{ '{' }} Client, {% for parameter in method.parameters.all %}{% if parameter.required %}{% if not loop.first %}{% endif %}{% if parameter.enumValues | length > 0%}{{parameter.enumName | caseUcfirst}},{% endif %}{% endif %}{% endfor %} {{service.name | caseUcfirst}} {{ '}' }} from "{{ language.params.npmPackage }}"; const client = new Client(); @@ -16,7 +16,7 @@ client {% endif %} {% if method.type == 'webAuth' %}// Go to OAuth provider login page {% endif %} -{% if method.type == 'webAuth' %}{% elseif method.type == 'location' %}const result = {% else %}const promise = {% endif %}{{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% for parameter in method.parameters.all %}{% if parameter.required %}{% if not loop.first %}, {% endif %}{{ parameter | paramExample }}{% endif %}{% endfor %}); +{% if method.type == 'webAuth' %}{% elseif method.type == 'location' %}const result = {% else %}const promise = {% endif %}{{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% for parameter in method.parameters.all %}{% if parameter.required %}{% if not loop.first %}, {% endif %}{% if parameter.enumValues | length > 0%}{{parameter.enumName}}.{{(parameter.enumKeys[0] ?? parameter.enumValues[0]) | caseUcfirst | replace({'-': ''}) }}{% else%}{{ parameter | paramExample }}{% endif %}{% endif %}{% endfor %}); {% if method.type == 'webAuth' %}{% elseif method.type == 'location' %}console.log(result); // Resource URL{% else %}promise.then(function (response) { console.log(response); // Success diff --git a/templates/web/src/enums/enum.ts.twig b/templates/web/src/enums/enum.ts.twig new file mode 100644 index 000000000..d9366b495 --- /dev/null +++ b/templates/web/src/enums/enum.ts.twig @@ -0,0 +1,6 @@ +export enum {{ enum.name | caseUcfirst }} { + {% for value in enum.enum %} + {% set key = enum.keys is empty ? value : enum.keys[loop.index0] %} + {{ key | caseUcfirst | replace({'-': ''})}} = '{{ value }}', + {% endfor %} +} \ No newline at end of file diff --git a/templates/web/src/index.ts.twig b/templates/web/src/index.ts.twig index 75ecd534d..dcee4ebc4 100644 --- a/templates/web/src/index.ts.twig +++ b/templates/web/src/index.ts.twig @@ -6,4 +6,7 @@ export type { Models, Payload, RealtimeResponseEvent, UploadProgress } from './c export type { QueryTypes, QueryTypesList } from './query'; export { Permission } from './permission'; export { Role } from './role'; -export { ID } from './id'; \ No newline at end of file +export { ID } from './id'; +{% for enum in enums %} +export { {{ enum | caseUcfirst }} } from './enums/{{enum | caseDash}}'; +{% endfor %} \ No newline at end of file