Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
51ce5b9
fix(web): partial arguments
TorstenDittmann Dec 14, 2021
6127659
Merge branch 'feat-db-refactor' of https://github.com/appwrite/sdk-ge…
TorstenDittmann Dec 14, 2021
173d4c4
delete old spec
TorstenDittmann Dec 14, 2021
ee6907e
Merge branch 'feat-db-refactor' of https://github.com/appwrite/sdk-ge…
TorstenDittmann Dec 14, 2021
47cf960
fix deno tests
TorstenDittmann Dec 15, 2021
ecaddbe
Merge branch 'feat-db-refactor' of https://github.com/appwrite/sdk-ge…
TorstenDittmann Jan 3, 2022
d3a982f
chore: update console spec
TorstenDittmann Jan 3, 2022
9c041ec
Merge branch 'master' of https://github.com/appwrite/sdk-generator in…
TorstenDittmann Mar 9, 2022
ac71cd2
fix: adapt to master
TorstenDittmann Mar 9, 2022
a01137e
fix: whitespace
TorstenDittmann Mar 9, 2022
401836c
Merge branch 'master' of https://github.com/appwrite/sdk-generator in…
TorstenDittmann Apr 27, 2022
89751a1
fix: update to latest master
TorstenDittmann Apr 27, 2022
161a491
revert changes
TorstenDittmann Apr 27, 2022
d2ec175
Merge branch 'master' of https://github.com/appwrite/sdk-generator in…
TorstenDittmann May 17, 2022
37164b4
Merge branch 'master' of https://github.com/appwrite/sdk-generator in…
TorstenDittmann May 31, 2022
acf3518
feat: make web oop
TorstenDittmann May 31, 2022
53ed5aa
tests: use proper node images for building web
TorstenDittmann May 31, 2022
3f53036
web: have hardcoded dependency versions
TorstenDittmann May 31, 2022
437a011
fix: tests
TorstenDittmann May 31, 2022
0080fbb
tests: downgrade playwright
TorstenDittmann May 31, 2022
ae559d8
tests: remove sudo npm install from web
TorstenDittmann May 31, 2022
1c67d71
web: fix typings reference
TorstenDittmann Jun 1, 2022
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
1 change: 0 additions & 1 deletion specs/swagger-appwrite-0.12.0.json

This file was deleted.

1 change: 1 addition & 0 deletions specs/swagger-appwrite.0.12.0.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion specs/swagger2-latest-console.json

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/SDK/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,13 @@ public function getParams()
{
return $this->params;
}

/**
* Language specific filters.
* @return array
*/
public function getFilters(): array
{
return [];
}
}
210 changes: 204 additions & 6 deletions src/SDK/Language/Web.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Appwrite\SDK\Language;

class Web extends JS {
use Twig\TwigFilter;

class Web extends JS
{

/**
* @return string
Expand All @@ -20,8 +23,38 @@ public function getFiles()
return [
[
'scope' => 'default',
'destination' => 'src/sdk.ts',
'template' => '/web/src/sdk.ts.twig',
'destination' => 'src/index.ts',
'template' => 'web/src/index.ts.twig',
'minify' => false,
],
[
'scope' => 'default',
'destination' => 'src/client.ts',
'template' => 'web/src/client.ts.twig',
'minify' => false,
],
[
'scope' => 'default',
'destination' => 'src/service.ts',
'template' => 'web/src/service.ts.twig',
'minify' => false,
],
[
'scope' => 'service',
'destination' => 'src/services/{{service.name | caseDash}}.ts',
'template' => 'web/src/services/template.ts.twig',
'minify' => false,
],
[
'scope' => 'default',
'destination' => 'src/models.ts',
'template' => 'web/src/models.ts.twig',
'minify' => false,
],
[
'scope' => 'default',
'destination' => 'src/query.ts',
'template' => 'web/src/query.ts.twig',
'minify' => false,
],
[
Expand Down Expand Up @@ -98,7 +131,7 @@ public function getParamExample(array $param)

$output = '';

if(empty($example) && $example !== 0 && $example !== false) {
if (empty($example) && $example !== 0 && $example !== false) {
switch ($type) {
case self::TYPE_NUMBER:
case self::TYPE_INTEGER:
Expand All @@ -118,8 +151,7 @@ public function getParamExample(array $param)
$output .= "document.getElementById('uploader').files[0]";
break;
}
}
else {
} else {
switch ($type) {
case self::TYPE_NUMBER:
case self::TYPE_INTEGER:
Expand All @@ -141,4 +173,170 @@ public function getParamExample(array $param)

return $output;
}

public function getTypeName($type, $method = []): string
{
switch ($type) {
case self::TYPE_INTEGER:
case self::TYPE_NUMBER:
return 'number';
break;
case self::TYPE_ARRAY:
return 'string[]';
case self::TYPE_FILE:
return 'File';
case self::TYPE_OBJECT:
if (empty($method)) {
return $type;
}

switch ($method['responseModel']) {
case 'user':
return "Partial<Preferences>";
break;
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 $type;
}

protected function populateGenerics(string $model, array $spec, array &$generics, bool $skipFirst = false)
{
if (!$skipFirst && $spec['definitions'][$model]['additionalProperties']) {
$generics[] = $this->toUpperCase($model);
}

$properties = $spec['definitions'][$model]['properties'];

foreach ($properties as $property) {
if (array_key_exists('sub_schema', $property) && $property['sub_schema']) {
$this->populateGenerics($property['sub_schema'], $spec, $generics, false);
}
}
}

public function getGenerics(string $model, array $spec, bool $skipFirst = false): string
{
$generics = [];

if (array_key_exists($model, $spec['definitions'])) {
$this->populateGenerics($model, $spec, $generics, $skipFirst);
}

if (empty($generics)) return '';

$generics = array_unique($generics);
$generics = array_map(fn ($type) => "{$type} extends Models.{$type}", $generics);

return '<' . implode(', ', $generics) . '>';
}

public function getReturn(array $method, array $spec): string
{
if ($method['type'] === 'webAuth') {
return 'void | URL';
} elseif ($method['type'] === 'location') {
return 'URL';
}

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->toUpperCase($method['responseModel']);

$models = [];

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

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

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

$ret .= '>';

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

return "";
}

public function toUpperCase(string $value): string
{
return ucfirst((string)$this->helperCamelCase($value));
}

protected function helperCamelCase($str)
{
$str = preg_replace('/[^a-z0-9' . implode("", []) . ']+/i', ' ', $str);
$str = trim($str);
$str = ucwords($str);
$str = str_replace(" ", "", $str);
$str = lcfirst($str);

return $str;
}

public function getSubSchema(array $property, array $spec): string
{
if (array_key_exists('sub_schema', $property)) {
$ret = '';
$generics = [];
$this->populateGenerics($property['sub_schema'], $spec, $generics);

$generics = array_filter($generics, fn ($model) => $model != $this->toUpperCase($property['sub_schema']));

$ret .= $this->toUpperCase($property['sub_schema']);
if (!empty($generics)) {
$ret .= '<' . implode(', ', $generics) . '>';
}
if ($property['type'] === 'array') {
$ret .= '[]';
}

return $ret;
}

return $this->getTypeName($property['type']);
}

public function getFilters(): array
{
return [
new TwigFilter('getPropertyType', function ($value, $method = []) {
return $this->getTypeName($value, $method);
}),
new TwigFilter('getSubSchema', function (array $property, array $spec) {
return $this->getSubSchema($property, $spec);
}),
new TwigFilter('getGenerics', function (string $model, array $spec, bool $skipAdditional = false) {
return $this->getGenerics($model, $spec, $skipAdditional);
}),
new TwigFilter('getReturn', function (array $method, array $spec) {
return $this->getReturn($method, $spec);
}),
];
}
}
8 changes: 8 additions & 0 deletions src/SDK/SDK.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ public function __construct(Language $language, Spec $spec)
$this->twig = new Environment(new FilesystemLoader(__DIR__ . '/../../templates'), [
'debug' => true
] );

/**
* Add language specific filters
*/
foreach ($this->language->getFilters() as $filter) {
$this->twig->addFilter($filter);
}

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

$this->twig->addFilter(new TwigFilter('caseLower', function ($value) {
Expand Down
12 changes: 6 additions & 6 deletions templates/web/package.json.twig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"./package.json": "./package.json"
},
"module": "dist/esm/sdk.js",
"types": "types/sdk.d.ts",
"types": "types/index.d.ts",
"repository": {
"type": "git",
"url": "{{ sdk.gitURL }}"
Expand All @@ -24,12 +24,12 @@
"build:libs": "rollup -c"
},
"devDependencies": {
"@rollup/plugin-typescript": "8.2.5",
"@rollup/plugin-typescript": "8.3.2",
"playwright": "1.15.0",
"rollup": "2.56.3",
"serve-handler": "6.1.3",
"tslib": "2.3.1",
"typescript": "4.2.3"
"rollup": "2.75.4",
"serve-handler": "6.1.0",
"tslib": "2.4.0",
"typescript": "4.7.2"
},
"dependencies": {
"cross-fetch": "3.1.5",
Expand Down
4 changes: 2 additions & 2 deletions templates/web/rollup.config.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import typescript from "@rollup/plugin-typescript";

export default {
external: Object.keys(pkg.dependencies),
input: "src/sdk.ts",
input: "src/index.ts",
plugins: [typescript()],
output: [
{
Expand All @@ -20,7 +20,7 @@ export default {
{
format: "iife",
file: pkg.jsdelivr,
name: "window",
name: "Appwrite",
extend: true,
globals: {
"cross-fetch": "window",
Expand Down
Loading