Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add types declaration in JS SDK #34

Merged
merged 1 commit into from
Apr 18, 2020
Merged
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
3 changes: 2 additions & 1 deletion example.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function getSSLPage($url) {
}

//$spec = getSSLPage('https://appwrite.io/v1/open-api-2.json?extensions=1');
$spec = getSSLPage('https://localhost:2444/v1/open-api-2.json?extensions=1'); // Enable only with Appwrite local server running on port 80
$spec = getSSLPage('https://appwrite.io/v1/open-api-2.json?extensions=1'); // Enable only with Appwrite local server running on port 80

if(empty($spec)) {
throw new Exception('Failed to fetch spec from Appwrite server');
Expand Down Expand Up @@ -80,6 +80,7 @@ function getSSLPage($url) {
$sdk
->setDescription('Repo description goes here')
->setShortDescription('Repo short description goes here')
->setVersion('0.0.0')
->setURL('https://example.com')
->setLogo('https://appwrite.io/v1/images/console.png')
->setLicenseContent('test test test')
Expand Down
18 changes: 16 additions & 2 deletions src/SDK/Language/JS.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
class JS extends Language {

protected $params = [
'npmPackage' => 'packageName',
'bowerPackage' => 'packageName',
'npmPackage' => 'appwrite',
'bowerPackage' => 'appwrite',
];

/**
Expand Down Expand Up @@ -164,6 +164,18 @@ public function getFiles()
'template' => '/js/docs/example.md.twig',
'minify' => false,
],
[
'scope' => 'default',
'destination' => 'types/index.d.ts',
'template' => '/js/types/index.d.ts.twig',
'minify' => false,
],
[
'scope' => 'default',
'destination' => 'tsconfig.json',
'template' => '/js/tsconfig.json.swig',
'minify' => false,
],
];
}

Expand All @@ -178,6 +190,8 @@ public function getTypeName($type)
case self::TYPE_NUMBER:
return 'number';
break;
case self::TYPE_ARRAY:
return 'string[]';
case self::TYPE_FILE:
return 'File';
break;
Expand Down
5 changes: 4 additions & 1 deletion templates/js/package.json.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
"version": "{{ sdk.version }}",
"license": "{{ sdk.license }}",
"main": "src/sdk.js",
"types": "types/index.d.ts",
"repository": {
"type": "git",
"url": "{{ sdk.gitURL }}"
},
"devDependencies": {},
"devDependencies": {
"typescript": "^3.6.4"
},
"dependencies": {}
}
8 changes: 8 additions & 0 deletions templates/js/tsconfig.json.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"target": "ES6",
"lib": ["ES2015", "ES6", "dom"],
"module": "commonjs",
"sourceMap": true
}
}
68 changes: 68 additions & 0 deletions templates/js/types/index.d.ts.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Type definitions for {{ language.params.npmPackage }} {{ sdk.version }}
// Project: {{ spec.title }}


/*~ This declaration specifies that the class constructor function
*~ is the exported object from the file
*/
export = {{ spec.title | caseUcfirst }};

/*~ Write your module's methods and properties in this class */
declare class {{ spec.title | caseUcfirst }} {
constructor();

/**
* @param {string} endpoint
* @returns {this}
*/
setEndpoint(endpoint: string): this;

{% for header in spec.global.headers %}
/**
* Set {{header.key | caseUcfirst}}
*
{% if header.description %}
{{header.description|comment2}}
*
{% endif %}
* @param value string
*
* @return this
*/
set{{header.key | caseUcfirst}}({{ header.key | caseCamel }}: string): this;
{% endfor %}

{% for service in spec.services %}
{{ service.name }}:{{ spec.title | caseUcfirst }}.{{ service.name | caseUcfirst }};
{% endfor %}

}

declare namespace {{ spec.title | caseUcfirst }} {

{% for service in spec.services %}
export interface {{ service.name | caseUcfirst }} {

{% for method in service.methods %}
/**
* {{ method.title }}
*
{% if method.description %}
{{ method.description|comment2 }}
{% endif %}
*
{% for parameter in method.parameters.all %}
* @param {{ '{' }}{{ parameter.type | typeName }}{{ '}' }} {{ parameter.name | caseCamel }}
{% endfor %}
* @throws {Error}
* @return {% if method.location %}{string}{% elseif method.cookies %}{null}{% else %}{Promise}{% endif %}

*/
{{ method.name | caseCamel }}({% for parameter in method.parameters.all %}{{ parameter.name | caseCamel }}: {{ parameter.type | typeName }}{% if not loop.last %}, {% endif %}{% endfor %}): Promise<string>;

{% endfor %}
}

{% endfor %}

}