Skip to content

Commit 4cabce3

Browse files
committed
chore: add javascript jsdocs
1 parent 59a6674 commit 4cabce3

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed

src/SDK/Language/CLI.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ public function getFiles(): array
201201
'destination' => 'lib/type-generation/languages/typescript.js',
202202
'template' => 'cli/lib/type-generation/languages/typescript.js.twig',
203203
],
204+
[
205+
'scope' => 'default',
206+
'destination' => 'lib/type-generation/languages/javascript.js',
207+
'template' => 'cli/lib/type-generation/languages/javascript.js.twig',
208+
],
204209
[
205210
'scope' => 'default',
206211
'destination' => 'lib/type-generation/languages/kotlin.js',

templates/cli/lib/commands/types.js.twig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const { Kotlin } = require("../type-generation/languages/kotlin");
1111
const { Swift } = require("../type-generation/languages/swift");
1212
const { Java } = require("../type-generation/languages/java");
1313
const { Dart } = require("../type-generation/languages/dart");
14+
const { JavaScript } = require("../type-generation/languages/javascript");
1415

1516
/**
1617
* @param {string} language
@@ -20,6 +21,8 @@ function createLanguageMeta(language) {
2021
switch (language) {
2122
case "ts":
2223
return new TypeScript();
24+
case "js":
25+
return new JavaScript();
2326
case "php":
2427
return new PHP();
2528
case "kotlin":
@@ -52,7 +55,7 @@ const typesLanguageOption = new Option(
5255
"-l, --language <language>",
5356
"The language of the types"
5457
)
55-
.choices(["auto", "ts", "php", "kotlin", "swift", "java", "dart"])
58+
.choices(["auto", "ts", "js", "php", "kotlin", "swift", "java", "dart"])
5659
.default("auto");
5760

5861
const typesCommand = actionRunner(async (rawOutputDirectory, {language}) => {
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/** @typedef {import('../attribute').Attribute} Attribute */
2+
const fs = require("fs");
3+
const path = require("path");
4+
5+
const { AttributeType } = require('../attribute');
6+
const { LanguageMeta } = require("./language");
7+
8+
class JavaScript extends LanguageMeta {
9+
getType(attribute) {
10+
let type = ""
11+
switch (attribute.type) {
12+
case AttributeType.STRING:
13+
case AttributeType.EMAIL:
14+
case AttributeType.DATETIME:
15+
case AttributeType.ENUM:
16+
case AttributeType.IP:
17+
case AttributeType.___URL:
18+
type = "string";
19+
break;
20+
case AttributeType.INTEGER:
21+
type = "number";
22+
break;
23+
case AttributeType.FLOAT:
24+
type = "number";
25+
break;
26+
case AttributeType.BOOLEAN:
27+
type = "boolean";
28+
break;
29+
case AttributeType.RELATIONSHIP:
30+
type = "any";
31+
break;
32+
default:
33+
throw new Error(`Unknown attribute type: ${attribute.type}`);
34+
}
35+
if (attribute.array) {
36+
type += "[]";
37+
}
38+
if (!attribute.required) {
39+
type += "|null";
40+
}
41+
return type;
42+
}
43+
44+
isSingleFile() {
45+
return true;
46+
}
47+
48+
_getAppwriteDependency() {
49+
if (fs.existsSync(path.resolve(process.cwd(), 'package.json'))) {
50+
const packageJsonRaw = fs.readFileSync(path.resolve(process.cwd(), 'package.json'));
51+
const packageJson = JSON.parse(packageJsonRaw.toString('utf-8'));
52+
return packageJson.dependencies['node-appwrite'] ? 'node-appwrite' : 'appwrite';
53+
}
54+
55+
return "appwrite";
56+
}
57+
58+
getTemplate() {
59+
return `/**
60+
* @typedef {import('${this._getAppwriteDependency()}').Models.Document} Document
61+
*/
62+
63+
<% for (const collection of collections) { %>
64+
/**
65+
* @typedef {Object} <%- toPascalCase(collection.name) %>
66+
<% for (const attribute of collection.attributes) { -%>
67+
* @property {<%- getType(attribute) %>} <%- toCamelCase(attribute.key) %>
68+
<% } -%>
69+
*/
70+
71+
<% } %>`;
72+
}
73+
74+
getFileName(_) {
75+
return "appwrite-types.js";
76+
}
77+
}
78+
79+
module.exports = { JavaScript };

templates/cli/lib/type-generation/languages/language.js.twig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,12 @@ const existsFiles = (...files) =>
8989
* @returns {string}
9090
*/
9191
function detectLanguage() {
92-
if (existsFiles("package.json", "tsconfig.json", "deno.json")) {
92+
if (existsFiles("tsconfig.json", "deno.json")) {
9393
return "ts";
9494
}
95+
if (existsFiles("package.json")) {
96+
return "js";
97+
}
9598
if (existsFiles("composer.json")) {
9699
return "php";
97100
}

0 commit comments

Comments
 (0)