Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/SDK/Language/Dart.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,35 @@ public function getFiles()
];
}

public function permissionHelperExample() {
return '[
Permission.read(Role.any()),
Permission.create(Role.user(ID.custom("6abdzeeq23dfds")),
Permission.delete(Role.team(ID.custom("editors"), "owner")),
]';
}

public function queriesHelperExample() {
return '[
Query.equal("title", ["Spiderman", "Dr Strange"]),
Query.greaterThan("year", 2000),
Query.orderAsc("title"),
Query.limit(5),
Query.offset(10)
]';
}

public function rolesHelperExample() {
return '[
Role.any(),
Roles.user(ID.custom("6abdzeeq23dfds"))
]';
}

public function uniqueIDExample() {
return 'ID.unique()';
}

public function getFilters(): array
{
return [
Expand All @@ -449,7 +478,7 @@ public function getFilters(): array
$value[$key] = " /// " . wordwrap($value[$key], 75, "\n /// ");
}
return implode("\n", $value);
}, ['is_safe' => ['html']]),
}, ['is_safe' => ['html']])
];
}
}
29 changes: 29 additions & 0 deletions src/SDK/Language/JS.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,33 @@ public function getParamDefault(array $param)

return $output;
}

public function permissionHelperExample() {
return '[
Permission.read(Role.any()),
Permission.create(Role.user(ID.custom("6abdzeeq23dfds")),
Permission.delete(Role.team(ID.custom("editors"), "owner")),
]';
}

public function queriesHelperExample() {
return '[
Query.equal("title", ["Spiderman", "Dr Strange"]),
Query.greaterThan("year", 2000),
Query.orderAsc("title"),
Query.limit(5),
Query.offset(10)
]';
}

public function rolesHelperExample() {
return '[
Role.any(),
Roles.user(ID.custom("6abdzeeq23dfds"))
]';
}

public function uniqueIDExample() {
return 'ID.unique()';
}
}
29 changes: 29 additions & 0 deletions src/SDK/Language/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,35 @@ protected function getReturn(array $method): string
return 'array';
}

public function permissionHelperExample() {
return '[
Permission::read(Role::any()),
Permission::create(Role::user(ID::custom("6abdzeeq23dfds")),
Permission::delete(Role::team(ID::custom("editors"), "owner")),
]';
}

public function queriesHelperExample() {
return '[
Query::equal("title", ["Spiderman", "Dr Strange"]),
Query::greaterThan("year", 2000),
Query::orderAsc("title"),
Query::limit(5),
Query::offset(10)
]';
}

public function rolesHelperExample() {
return '[
Role::any(),
Roles::user(ID::custom("6abdzeeq23dfds"))
]';
}

public function uniqueIDExample() {
return 'ID::unique()';
}

public function getFilters(): array {
return [
new TwigFilter('getReturn', function($value) {
Expand Down
8 changes: 5 additions & 3 deletions src/SDK/SDK.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@ public function __construct(Language $language, Spec $spec)
foreach ($this->language->getFilters() as $filter) {
$this->twig->addFilter($filter);
}

$this->twig->addExtension(new \Twig\Extension\DebugExtension());

$this->twig->addFilter(new TwigFilter('customLanguageFilter', function($value, $filter) use ($language) {
return call_user_func([$language, $filter], $value);
}, ['is_safe' => ['html']]));

$this->twig->addFilter(new TwigFilter('caseLower', function ($value) {
return strtolower((string)$value);
}));
Expand Down Expand Up @@ -556,7 +560,6 @@ public function generate($target)
foreach ($this->spec->getServices() as $key => $service) {
$methods = $this->spec->getMethods($key);
$params['service'] = [
'globalParams' => $service['globalParams'] ?? [],
'description' => $service['description'] ?? '',
'name' => $key,
'features' => [
Expand Down Expand Up @@ -584,7 +587,6 @@ public function generate($target)
$params['service'] = [
'name' => $key,
'methods' => $methods,
'globalParams' => $service['globalParams'] ?? [],
'features' => [
'upload' => $this->hasUploads($methods),
'location' => $this->hasLocation($methods),
Expand Down
11 changes: 10 additions & 1 deletion src/Spec/Swagger2.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public function getMethods($service)
'path' => [],
'query' => [],
'body' => [],
'example' => [],
],
'emptyResponse' => $emptyResponse,
'responseModel' => $responseModel,
Expand All @@ -192,7 +193,7 @@ public function getMethods($service)
}

$method['parameters'] = (isset($method['parameters']) && is_array($method['parameters'])) ? $method['parameters'] : [];

$exampleParams = $method['x-example-params'] ?? [];
foreach ($method['parameters'] as $parameter) {
$param = [
'name' => $parameter['name'] ?? null,
Expand All @@ -202,6 +203,7 @@ public function getMethods($service)
'required' => $parameter['required'] ?? false,
'default' => $parameter['default'] ?? null,
'example' => $parameter['x-example'] ?? null,
'exampleFilter' => $parameter['x-example-filter'] ?? null,
'isUploadID' => $parameter['x-upload-id'] ?? false,
'array' => [
'type' => $parameter['items']['type'] ?? '',
Expand Down Expand Up @@ -239,6 +241,7 @@ public function getMethods($service)
$param['default'] = $value['default'] ?? null;
$param['example'] = $value['x-example'] ?? null;
$param['isUploadID'] = $value['x-upload-id'] ?? false;
$param['exampleFilter'] = $value['x-example-filter'] ?? null;
$param['array'] = [
'type' => $value['items']['type'] ?? '',
];
Expand All @@ -250,6 +253,9 @@ public function getMethods($service)

$output['parameters']['body'][] = $param;
$output['parameters']['all'][] = $param;
if($param['required'] || in_array($param['name'], $exampleParams)) {
$output['parameters']['example'][] = $param;
}
}

continue 2;
Expand All @@ -258,6 +264,9 @@ public function getMethods($service)
}

$output['parameters']['all'][] = $param;
if($param['required'] || in_array($param['name'], $exampleParams)) {
$output['parameters']['example'][] = $param;
}
}

usort($output['parameters']['all'], function ($a, $b) {
Expand Down
10 changes: 4 additions & 6 deletions templates/dart/docs/example.md.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:{{ language.params.packageName }}/{{ language.params.packageName

void main() { // Init SDK
Client client = Client();
{{ service.name | caseUcfirst }} {{ service.name | caseCamel }} = {{service.name | caseUcfirst}}(client{% if service.globalParams | length %}{% for parameter in service.globalParams %}, {{ parameter.name | caseCamel | overrideIdentifier }}: {{ parameter | paramExample }}{% endfor %}{% endif %});
{{ service.name | caseUcfirst }} {{ service.name | caseCamel }} = {{service.name | caseUcfirst}}(client);

{% if method.security|length > 0 %}
client
Expand All @@ -17,14 +17,12 @@ void main() { // Init SDK
{% endfor %} ;

{% endif %}
Future result = {{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% for parameter in method.parameters.all %}
Future result = {{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% for parameter in method.parameters.example %}
{% if loop.first %}

{% endif %}
{% if parameter.required %}
{{ parameter.name | caseCamel | overrideIdentifier }}: {{ parameter | paramExample | replace({'<field-name>': (parameter.name | caseCamel) }) | raw }},
{% endif %}
{% endfor %}{% if method.parameters.all | length > 0 %} {% endif %});
{{ parameter.name | caseCamel | overrideIdentifier }}: {% if parameter.exampleFilter %}{{ parameter | customLanguageFilter(parameter.exampleFilter) }}{% else %}{{ parameter | paramExample | replace({'<field-name>': (parameter.name | caseCamel) }) | raw }}{% endif %},
{% endfor %}{% if method.parameters.example | length > 0 %} {% endif %});

result
.then((response) {
Expand Down
10 changes: 8 additions & 2 deletions templates/deno/docs/example.md.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const fs = require('fs');
// Init SDK
let client = new sdk.Client();

let {{ service.name | caseCamel }} = new sdk.{{service.name | caseUcfirst}}(client{% if service.globalParams | length %}{% for parameter in service.globalParams %}, {{ parameter | paramExample }}{% endfor %}{% endif %});
let {{ service.name | caseCamel }} = new sdk.{{service.name | caseUcfirst}}(client);

{% if method.security|length > 0 %}
client
Expand All @@ -19,7 +19,13 @@ 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 }}({% if method.parameters.example | length > 0 %}

{% endif %}
{% for parameter in method.parameters.example %}
{% if parameter.exampleFilter %}{{ parameter | customLanguageFilter(parameter.exampleFilter) }}{% else %}{{ parameter | paramExample }}{% endif %}{% if not loop.last %}, {% endif %}

{% endfor %});

promise.then(function (response) {
console.log(response);
Expand Down
2 changes: 1 addition & 1 deletion templates/dotnet/docs/example.md.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ client
{% endfor %};

{% endif %}
{{ service.name | caseUcfirst }} {{ service.name | caseCamel }} = new {{ service.name | caseUcfirst }}(client{% if service.globalParams | length %}{% for parameter in service.globalParams %}, {{ parameter | paramExample }}{% endfor %}{% endif %});
{{ service.name | caseUcfirst }} {{ service.name | caseCamel }} = new {{ service.name | caseUcfirst }}(client);

{% if method.type == 'location' %}string{% else %}HttpResponseMessage{% endif %} result = await {{ service.name | caseCamel }}.{{ method.name | caseUcfirst }}({% for parameter in method.parameters.all %}{% if parameter.required %}{% if not loop.first %}, {% endif %}{{ parameter | paramExample }}{% endif %}{% endfor %});
26 changes: 10 additions & 16 deletions templates/flutter/docs/example.md.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:{{ language.params.packageName }}/{{ language.params.packageName

void main() { // Init SDK
Client client = Client();
{{ service.name | caseUcfirst }} {{ service.name | caseCamel }} = {{service.name | caseUcfirst}}(client{% if service.globalParams | length %}{% for parameter in service.globalParams %}, {{ parameter.name | caseCamel | overrideIdentifier }}: {{ parameter | paramExample }}{% endfor %}{% endif %});
{{ service.name | caseUcfirst }} {{ service.name | caseCamel }} = {{service.name | caseUcfirst}}(client);

{% if method.security|length > 0 %}
client
Expand All @@ -18,14 +18,12 @@ void main() { // Init SDK
{% endif %}
{% if method.type == 'location' %}
// downloading file
Future result = {{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% for parameter in method.parameters.all %}
Future result = {{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% for parameter in method.parameters.example %}
{% if loop.first %}

{% endif %}
{% if parameter.required %}
{{ parameter.name | caseCamel }}: {{ parameter | paramExample }},
{% endif %}
{% endfor %}{% if method.parameters.all | length > 0 %} {% endif %}).then((bytes) {
{{ parameter.name | caseCamel | overrideIdentifier }}: {% if parameter.exampleFilter %}{{ parameter | customLanguageFilter(parameter.exampleFilter) }}{% else %}{{ parameter | paramExample }}{% endif %},
{% endfor %}{% if method.parameters.example | length > 0 %} {% endif %}).then((bytes) {
final file = File('path_to_file/filename.ext');
file.writeAsBytesSync(bytes)
}).catchError((error) {
Expand All @@ -35,14 +33,12 @@ void main() { // Init SDK

//displaying image preview
FutureBuilder(
future: {{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% for parameter in method.parameters.all %}
future: {{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% for parameter in method.parameters.example %}
{% if loop.first %}

{% endif %}
{% if parameter.required %}
{{ parameter.name | caseCamel | overrideIdentifier }}: {{ parameter | paramExample }},
{% endif %}
{% endfor %}{% if method.parameters.all | length > 0 %} {% endif %}
{{ parameter.name | caseCamel | overrideIdentifier }}: {% if parameter.exampleFilter %}{{ parameter | customLanguageFilter(parameter.exampleFilter) }}{% else %}{{ parameter | paramExample }}{% endif %},
{% endfor %}{% if method.parameters.example | length > 0 %} {% endif %}
), //works for both public file and private file, for private files you need to be logged in
builder: (context, snapshot) {
return snapshot.hasData && snapshot.data != null
Expand All @@ -53,14 +49,12 @@ FutureBuilder(
},
);
{% else %}
Future result = {{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% for parameter in method.parameters.all %}
Future result = {{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% for parameter in method.parameters.example %}
{% if loop.first %}

{% endif %}
{% if parameter.required %}
{{ parameter.name | caseCamel }}: {{ parameter | paramExample }},
{% endif %}
{% endfor %}{% if method.parameters.all | length > 0 %} {% endif %});
{{ parameter.name | caseCamel | overrideIdentifier }}: {% if parameter.exampleFilter %}{{ parameter | customLanguageFilter(parameter.exampleFilter) }}{% else %}{{ parameter | paramExample }}{% endif %},
{% endfor %}{% if method.parameters.example | length > 0 %} {% endif %});

result
.then((response) {
Expand Down
2 changes: 1 addition & 1 deletion templates/kotlin/docs/java/example.md.twig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void main() {
.set{{header | caseUcfirst}}("{{node[header]['x-appwrite']['demo']}}"){% if loop.last %};{% endif %} // {{node[header].description}}
{% endfor %}{% endfor %}{% endif %}

{{ service.name | caseUcfirst }} {{ service.name | caseCamel }} = new {{ service.name | caseUcfirst }}(client{% if service.globalParams | length %}{% for parameter in service.globalParams %}, {{ parameter | paramExample }}{% endfor %}{% endif %});
{{ service.name | caseUcfirst }} {{ service.name | caseCamel }} = new {{ service.name | caseUcfirst }}(client);
{{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% if method.parameters.all | length == 0 %}new Continuation<Response>() {
@NotNull
@Override
Expand Down
2 changes: 1 addition & 1 deletion templates/kotlin/docs/kotlin/example.md.twig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ suspend fun main() {
.set{{header | caseUcfirst}}("{{node[header]['x-appwrite']['demo']}}") // {{node[header].description}}
{% endfor %}{% endfor %}{% endif %}

val {{ service.name | caseCamel }} = {{ service.name | caseUcfirst }}(client{% if service.globalParams | length %}{% for parameter in service.globalParams %}, {{ parameter | paramExample }}{% endfor %}{% endif %})
val {{ service.name | caseCamel }} = {{ service.name | caseUcfirst }}(client)
{% if method.type == 'webAuth' %} {% elseif method.type == 'location' %} val result = {% else %} val response = {% endif %}{{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% if method.parameters.all | length == 0 %}){% endif %}

{% for parameter in method.parameters.all %}
Expand Down
10 changes: 8 additions & 2 deletions templates/node/docs/example.md.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const fs = require('fs');
// Init SDK
const client = new sdk.Client();

const {{ service.name | caseCamel }} = new sdk.{{service.name | caseUcfirst}}(client{% if service.globalParams | length %}{% for parameter in service.globalParams %}, {{ parameter | paramExample }}{% endfor %}{% endif %});
const {{ service.name | caseCamel }} = new sdk.{{service.name | caseUcfirst}}(client);

{% if method.security|length > 0 %}
client
Expand All @@ -18,7 +18,13 @@ 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 }}({% if method.parameters.example | length > 0 %}

{% endif %}
{% for parameter in method.parameters.example %}
{% if parameter.exampleFilter %}{{ parameter | customLanguageFilter(parameter.exampleFilter) }}{% else %}{{ parameter | paramExample }}{% endif %}{% if not loop.last %}, {% endif %}

{% endfor %});

promise.then(function (response) {
console.log(response);
Expand Down
21 changes: 1 addition & 20 deletions templates/node/index.d.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -176,27 +176,8 @@ declare module "{{ language.params.npmPackage|caseDash }}" {

{% for service in spec.services %}
export class {{ service.name | caseUcfirst }} extends Service {
constructor(client: Client{% for parameter in service.globalParams %}, {{ parameter.name | caseCamel | escapeKeyword }}: {{ parameter | typeName }}{% if not parameter.required %}|null{% endif %}{% endfor %});

{% if service.globalParams | length %}
{% for parameter in service.globalParams %}
/**
* Get {{ parameter.name }}.
*
* @returns {{ '{' }}{{ parameter | typeName }}{{ '}' }}
*/
get{{ parameter.name | caseUcfirst | escapeKeyword }}(): {{ parameter | typeName }};

/**
* Set {{ parameter.name }}.
*
* @param {{ '{' }}{{ parameter | typeName }}{{ '}' }} {{ parameter.name | caseCamel | escapeKeyword }}
* @returns {void}
*/
set{{ parameter.name | caseUcfirst | escapeKeyword }}({{ parameter.name | caseCamel | escapeKeyword }}: {{ parameter | typeName }}): void;
constructor(client: Client);

{% endfor %}
{% endif %}
{% for method in service.methods %}
{% set generics = _self.get_generics_method(spec.definitions[method.responseModel], spec, true, true) %}
{% set generics_return = _self.get_generics_method_return(spec.definitions[method.responseModel], spec) %}
Expand Down
Loading