Skip to content

Commit

Permalink
Merge pull request #799 from appwrite/feat-node-sdk-typescript
Browse files Browse the repository at this point in the history
feat: node-appwrite use ts
  • Loading branch information
christyjacob4 authored Apr 17, 2024
2 parents b4c6327 + 5e9c5d4 commit d875e74
Show file tree
Hide file tree
Showing 27 changed files with 642 additions and 1,180 deletions.
207 changes: 103 additions & 104 deletions src/SDK/Language/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Appwrite\SDK\Language;

class Node extends JS
class Node extends Web
{
/**
* @return string
Expand All @@ -12,31 +12,84 @@ public function getName(): string
return 'NodeJS';
}

/**
* @param array $parameter
* @param array $spec
* @return string
*/
public function getTypeName(array $parameter, array $spec = []): string
public function getTypeName(array $parameter, array $method = []): string
{
if (isset($parameter['enumName'])) {
return \ucfirst($parameter['enumName']);
}
if (!empty($parameter['enumValues'])) {
return \ucfirst($parameter['name']);
}
return match ($parameter['type']) {
self::TYPE_INTEGER,
self::TYPE_NUMBER => 'number',
self::TYPE_STRING => 'string',
self::TYPE_FILE => 'InputFile',
self::TYPE_BOOLEAN => 'boolean',
self::TYPE_OBJECT => 'object',
self::TYPE_ARRAY => (!empty(($parameter['array'] ?? [])['type']) && !\is_array($parameter['array']['type']))
? $this->getTypeName($parameter['array']) . '[]'
: 'string[]',
default => $parameter['type'],
};
switch ($parameter['type']) {
case self::TYPE_INTEGER:
case self::TYPE_NUMBER:
return 'number';
case self::TYPE_ARRAY:
if (!empty(($parameter['array'] ?? [])['type']) && !\is_array($parameter['array']['type'])) {
return $this->getTypeName($parameter['array']) . '[]';
}
return 'string[]';
case self::TYPE_FILE:
return "File";
case self::TYPE_OBJECT:
if (empty($method)) {
return $parameter['type'];
}
switch ($method['responseModel']) {
case 'user':
return "Partial<Preferences>";
case 'document':
if ($method['method'] === 'post') {
return "Omit<Document, keyof Models.Document>";
}
if ($method['method'] === 'patch') {
return "Partial<Omit<Document, keyof Models.Document>>";
}
}
break;
}
return $parameter['type'];
}

public function getReturn(array $method, array $spec): string
{
if ($method['type'] === 'webAuth') {
return 'Promise<string>';
}

if ($method['type'] === 'location') {
return 'Promise<ArrayBuffer>';
}

if (array_key_exists('responseModel', $method) && !empty($method['responseModel']) && $method['responseModel'] !== 'any') {
$ret = 'Promise<';

if (
array_key_exists($method['responseModel'], $spec['definitions']) &&
array_key_exists('additionalProperties', $spec['definitions'][$method['responseModel']]) &&
!$spec['definitions'][$method['responseModel']]['additionalProperties']
) {
$ret .= 'Models.';
}

$ret .= $this->toPascalCase($method['responseModel']);

$models = [];

$this->populateGenerics($method['responseModel'], $spec, $models);

$models = array_unique($models);
$models = array_filter($models, fn ($model) => $model != $this->toPascalCase($method['responseModel']));

if (!empty($models)) {
$ret .= '<' . implode(', ', $models) . '>';
}

$ret .= '>';

return $ret;
}
return 'Promise<{}>';
}

/**
Expand All @@ -47,53 +100,48 @@ public function getFiles(): array
return [
[
'scope' => 'default',
'destination' => 'index.js',
'template' => 'node/index.js.twig',
'destination' => 'src/index.ts',
'template' => 'node/src/index.ts.twig',
],
[
'scope' => 'default',
'destination' => 'index.d.ts',
'template' => 'node/index.d.ts.twig',
'destination' => 'src/client.ts',
'template' => 'node/src/client.ts.twig',
],
[
'scope' => 'default',
'destination' => 'lib/client.js',
'template' => 'node/lib/client.js.twig',
'destination' => 'src/inputFile.ts',
'template' => 'node/src/inputFile.ts.twig',
],
[
'scope' => 'default',
'destination' => 'lib/permission.js',
'template' => 'node/lib/permission.js.twig',
'scope' => 'service',
'destination' => 'src/services/{{service.name | caseDash}}.ts',
'template' => 'node/src/services/template.ts.twig',
],
[
'scope' => 'default',
'destination' => 'lib/role.js',
'template' => 'node/lib/role.js.twig',
'destination' => 'src/models.ts',
'template' => 'web/src/models.ts.twig',
],
[
'scope' => 'default',
'destination' => 'lib/id.js',
'template' => 'node/lib/id.js.twig',
'destination' => 'src/permission.ts',
'template' => 'web/src/permission.ts.twig',
],
[
'scope' => 'default',
'destination' => 'lib/query.js',
'template' => 'node/lib/query.js.twig',
'destination' => 'src/role.ts',
'template' => 'web/src/role.ts.twig',
],
[
'scope' => 'default',
'destination' => 'lib/inputFile.js',
'template' => 'node/lib/inputFile.js.twig',
'destination' => 'src/id.ts',
'template' => 'web/src/id.ts.twig',
],
[
'scope' => 'default',
'destination' => '/lib/service.js',
'template' => 'node/lib/service.js.twig',
],
[
'scope' => 'service',
'destination' => '/lib/services/{{service.name | caseDash}}.js',
'template' => 'node/lib/services/service.js.twig',
'destination' => 'src/query.ts',
'template' => 'web/src/query.ts.twig',
],
[
'scope' => 'default',
Expand All @@ -115,80 +163,31 @@ public function getFiles(): array
'destination' => 'package.json',
'template' => 'node/package.json.twig',
],
[
'scope' => 'default',
'destination' => 'lib/exception.js',
'template' => 'node/lib/exception.js.twig',
],
[
'scope' => 'method',
'destination' => 'docs/examples/{{service.name | caseLower}}/{{method.name | caseDash}}.md',
'template' => 'node/docs/example.md.twig',
],
[
'scope' => 'default',
'destination' => 'tsconfig.json',
'template' => '/node/tsconfig.json.twig',
],
[
'scope' => 'default',
'destination' => 'tsup.config.js',
'template' => 'node/tsup.config.js.twig',
],
[
'scope' => 'default',
'destination' => '.travis.yml',
'template' => 'node/.travis.yml.twig',
],
[
'scope' => 'enum',
'destination' => 'lib/enums/{{ enum.name | caseDash }}.js',
'template' => 'node/lib/enums/enum.js.twig',
'destination' => 'src/enums/{{ enum.name | caseDash }}.ts',
'template' => 'web/src/enums/enum.ts.twig',
],
];
}

/**
* @param array $param
* @return string
*/
public function getParamExample(array $param): string
{
$type = $param['type'] ?? '';
$example = $param['example'] ?? '';

$output = '';

if (empty($example) && $example !== 0 && $example !== false) {
switch ($type) {
case self::TYPE_NUMBER:
case self::TYPE_INTEGER:
case self::TYPE_BOOLEAN:
$output .= 'null';
break;
case self::TYPE_STRING:
$output .= "''";
break;
case self::TYPE_ARRAY:
$output .= '[]';
break;
case self::TYPE_OBJECT:
$output .= '{}';
break;
case self::TYPE_FILE:
$output .= "InputFile.fromPath('/path/to/file.png', 'file.png')";
break;
}
} else {
switch ($type) {
case self::TYPE_NUMBER:
case self::TYPE_INTEGER:
case self::TYPE_ARRAY:
case self::TYPE_OBJECT:
$output .= $example;
break;
case self::TYPE_BOOLEAN:
$output .= ($example) ? 'true' : 'false';
break;
case self::TYPE_STRING:
$output .= "'{$example}'";
break;
case self::TYPE_FILE:
$output .= "InputFile.fromPath('/path/to/file.png', 'file.png')";
break;
}
}

return $output;
}
}
23 changes: 0 additions & 23 deletions templates/node/base/params.twig

This file was deleted.

8 changes: 0 additions & 8 deletions templates/node/base/requests/api.twig

This file was deleted.

Loading

0 comments on commit d875e74

Please sign in to comment.