-
Notifications
You must be signed in to change notification settings - Fork 193
feat: sdk for md #1265
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
Open
ChiragAgg5k
wants to merge
1
commit into
master
Choose a base branch
from
docs-sdk
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: sdk for md #1265
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,208 @@ | ||
| <?php | ||
|
|
||
| namespace Appwrite\SDK\Language; | ||
|
|
||
| use Appwrite\SDK\Language; | ||
| use Twig\TwigFilter; | ||
|
|
||
| class Docs extends Language | ||
| { | ||
| protected $params = []; | ||
|
|
||
| /** | ||
| * @return string | ||
| */ | ||
| public function getName(): string | ||
| { | ||
| return 'Docs'; | ||
| } | ||
|
|
||
| /** | ||
| * @return array | ||
| */ | ||
| public function getKeywords(): array | ||
| { | ||
| // Docs don't need keyword escaping | ||
| return []; | ||
| } | ||
|
|
||
| /** | ||
| * @return array | ||
| */ | ||
| public function getIdentifierOverrides(): array | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| public function getStaticAccessOperator(): string | ||
| { | ||
| return '.'; | ||
| } | ||
|
|
||
| public function getStringQuote(): string | ||
| { | ||
| return "'"; | ||
| } | ||
|
|
||
| public function getArrayOf(string $elements): string | ||
| { | ||
| return '[' . $elements . ']'; | ||
| } | ||
|
|
||
| /** | ||
| * @return array | ||
| */ | ||
| public function getFiles(): array | ||
| { | ||
| return [ | ||
| [ | ||
| 'scope' => 'method', | ||
| 'destination' => 'typescript/{{ service.name | caseLower }}/{{ method.name | caseKebab }}.md', | ||
| 'template' => 'docs/typescript/method.md.twig', | ||
| ] | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @param array $parameter | ||
| * @return string | ||
| */ | ||
| public function getTypeName(array $parameter, array $spec = []): string | ||
| { | ||
| // For TypeScript/JavaScript-like languages | ||
| if (isset($parameter['enumName'])) { | ||
| return \ucfirst($parameter['enumName']); | ||
| } | ||
| if (!empty($parameter['enumValues'])) { | ||
| return \ucfirst($parameter['name']); | ||
| } | ||
| if (isset($parameter['items'])) { | ||
| $parameter['array'] = $parameter['items']; | ||
| } | ||
| 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 'any[]'; | ||
| case self::TYPE_FILE: | ||
| return 'File'; | ||
| case self::TYPE_OBJECT: | ||
| return 'object'; | ||
| } | ||
| return $parameter['type']; | ||
| } | ||
|
|
||
| /** | ||
| * @param array $param | ||
| * @return string | ||
| */ | ||
| public function getParamDefault(array $param): string | ||
| { | ||
| $type = $param['type'] ?? ''; | ||
| $default = $param['default'] ?? ''; | ||
| $required = $param['required'] ?? false; | ||
|
|
||
| if ($required) { | ||
| return ''; | ||
| } | ||
|
|
||
| if (!empty($default)) { | ||
| return ' = ' . $default; | ||
| } | ||
|
|
||
| return match ($type) { | ||
| self::TYPE_ARRAY => ' = []', | ||
| self::TYPE_OBJECT => ' = {}', | ||
| default => ' = null', | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * @param array $param | ||
| * @param string $lang | ||
| * @return string | ||
| */ | ||
| public function getParamExample(array $param, string $lang = ''): string | ||
| { | ||
| $type = $param['type'] ?? ''; | ||
| $example = $param['example'] ?? ''; | ||
|
|
||
| $hasExample = !empty($example) || $example === 0 || $example === false; | ||
|
|
||
| if (!$hasExample) { | ||
| return match ($type) { | ||
| self::TYPE_ARRAY => '[]', | ||
| self::TYPE_FILE => 'file', | ||
| self::TYPE_INTEGER, self::TYPE_NUMBER => '0', | ||
| self::TYPE_BOOLEAN => 'false', | ||
| self::TYPE_OBJECT => '{}', | ||
| self::TYPE_STRING => "''", | ||
| }; | ||
|
Comment on lines
+137
to
+144
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing default case in match expression. If return match ($type) {
self::TYPE_ARRAY => '[]',
self::TYPE_FILE => 'file',
self::TYPE_INTEGER, self::TYPE_NUMBER => '0',
self::TYPE_BOOLEAN => 'false',
self::TYPE_OBJECT => '{}',
self::TYPE_STRING => "''",
+ default => "''",
};🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| return match ($type) { | ||
| self::TYPE_ARRAY => $this->isPermissionString($example) ? $this->getPermissionExample($example) : $example, | ||
| self::TYPE_INTEGER, self::TYPE_NUMBER => (string)$example, | ||
| self::TYPE_FILE => 'file', | ||
| self::TYPE_BOOLEAN => ($example) ? 'true' : 'false', | ||
| self::TYPE_OBJECT => ($example === '{}') | ||
| ? '{}' | ||
| : (($formatted = json_encode(json_decode($example, true), JSON_PRETTY_PRINT)) | ||
| ? $formatted | ||
| : $example), | ||
| self::TYPE_STRING => "'{$example}'", | ||
| }; | ||
| } | ||
|
|
||
| public function getPermissionExample(string $example): string | ||
| { | ||
| $permissions = $this->extractPermissionParts($example); | ||
| $result = []; | ||
|
|
||
| foreach ($permissions as $permission) { | ||
| $action = ucfirst($permission['action']); | ||
| $role = ucfirst($this->toCamelCase($permission['role'])); | ||
|
|
||
| if ($permission['id'] !== null) { | ||
| if ($permission['innerRole'] !== null) { | ||
| $result[] = "Permission.{$action}(Role.{$role}('{$permission['id']}', '{$permission['innerRole']}'))"; | ||
| } else { | ||
| $result[] = "Permission.{$action}(Role.{$role}('{$permission['id']}'))"; | ||
| } | ||
| } else { | ||
| $result[] = "Permission.{$action}(Role.{$role}())"; | ||
| } | ||
| } | ||
|
|
||
| return '[' . implode(', ', $result) . ']'; | ||
| } | ||
|
|
||
| public function getFilters(): array | ||
| { | ||
| return [ | ||
| new TwigFilter('getPropertyType', function ($value, $method = []) { | ||
| return $this->getTypeName($value, $method); | ||
| }), | ||
| new TwigFilter('comment', function ($value) { | ||
| $value = explode("\n", $value); | ||
| foreach ($value as $key => $line) { | ||
| $value[$key] = wordwrap($line, 80, "\n"); | ||
| } | ||
| return implode("\n", $value); | ||
| }, ['is_safe' => ['html']]), | ||
| new TwigFilter('caseEnumKey', function (string $value) { | ||
| return $this->toPascalCase($value); | ||
| }), | ||
| new TwigFilter('getResponseModel', function (array $method) { | ||
| if (!empty($method['responseModel']) && $method['responseModel'] !== 'any') { | ||
| return 'Models.' . \ucfirst($method['responseModel']); | ||
| } | ||
| return null; | ||
| }), | ||
| ]; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # {{ method.name | caseCamel }} | ||
| {% if method.deprecated %} | ||
|
|
||
| ⚠️ **DEPRECATED**{% if method.since %} since {{ method.since }}{% endif %}{% if method.replaceWith %} - Use `{{ method.replaceWith }}` instead{% endif %} | ||
|
|
||
| {% endif %} | ||
|
|
||
| Description: {{ method.description | raw | trim }} | ||
|
|
||
| ## Parameters | ||
| {% if method.parameters.all | length > 0 %} | ||
|
|
||
| | Parameter | Type | Required | Description | | ||
| |-----------|------|----------|-------------| | ||
| {% for parameter in method.parameters.all -%} | ||
| | `{{ parameter.name | caseCamel }}` | `{{ parameter | getPropertyType }}` | {% if parameter.required %}✅{% else %}❌{% endif %} | {{ parameter.description | raw }}{% if parameter.default %} (Default: `{{ parameter.default }}`){% endif %}{% if parameter.enumValues | length > 0 %}<br>**Allowed:** {% for value in parameter.enumValues %}`{{ value }}`{% if not loop.last %}, {% endif %}{% endfor %}{% endif %} | | ||
| {% endfor %} | ||
| {%- else %} | ||
|
|
||
| This method does not accept any parameters. | ||
| {% endif %} | ||
|
|
||
| ## Usage | ||
|
|
||
| ```typescript | ||
| import { Client, {{ service.name | caseUcfirst }}{% for parameter in method.parameters.all %}{% if parameter.enumValues | length > 0 %}, {{ parameter.enumName | caseUcfirst }}{% endif %}{% endfor %}{% if method | getResponseModel %}, Models{% endif %} } from '{{ spec.title | caseLower }}'; | ||
|
|
||
| const client = new Client() | ||
| .setEndpoint('{{ spec.endpointDocs | raw }}') | ||
| {%- if method.auth|length > 0 %} | ||
| {% for node in method.auth -%} | ||
| {% for key,header in node|keys -%} | ||
| .set{{header}}('{{ node[header]['x-appwrite']['demo'] | raw }}') | ||
| {%- endfor %} | ||
| {%- endfor -%} | ||
| {% endif -%} | ||
| ; | ||
|
|
||
| const {{ service.name | caseCamel }} = new {{ service.name | caseUcfirst }}(client); | ||
| {% if method.type == 'location' %} | ||
| const result{% if method | getResponseModel %}: {{ method | getResponseModel }}{% endif %} = {{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% if method.parameters.all | length == 0 %}); | ||
| {% else -%} | ||
| { | ||
| {% for parameter in method.parameters.all %} | ||
| {{ parameter.name | caseCamel }}: {% if parameter.enumValues | length > 0 %}{{ parameter.enumName | caseUcfirst }}.{{ (parameter.enumKeys[0] ?? parameter.enumValues[0]) | caseEnumKey }}{% else %}{{ parameter | paramExample }}{% endif %}, | ||
| {% endfor -%} | ||
| }); | ||
| {% endif -%} | ||
| {% elseif method.type != 'webAuth' %} | ||
| const result{% if method | getResponseModel %}: {{ method | getResponseModel }}{% endif %} = await {{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% if method.parameters.all | length == 0 %}); | ||
| {% else -%} | ||
| { | ||
| {% for parameter in method.parameters.all %} | ||
| {{ parameter.name | caseCamel }}: {% if parameter.enumValues | length > 0 %}{{ parameter.enumName | caseUcfirst }}.{{ (parameter.enumKeys[0] ?? parameter.enumValues[0]) | caseEnumKey }}{% else %}{{ parameter | paramExample }}{% endif %}, | ||
| {% endfor -%} | ||
| }); | ||
| {% endif -%} | ||
| {% else -%} | ||
| {{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% if method.parameters.all | length == 0 %}); | ||
| {% else -%} | ||
| { | ||
| {% for parameter in method.parameters.all %} | ||
| {{ parameter.name | caseCamel }}: {% if parameter.enumValues | length > 0 %}{{ parameter.enumName | caseUcfirst }}.{{ (parameter.enumKeys[0] ?? parameter.enumValues[0]) | caseEnumKey }}{% else %}{{ parameter | paramExample }}{% endif %}, | ||
| {% endfor -%} | ||
| }); | ||
| {% endif -%} | ||
| {% endif -%} | ||
| ``` | ||
| {% if method | getResponseModel %} | ||
|
|
||
| ## Response Model | ||
|
|
||
| Returns a `{{ method | getResponseModel }}` object with the following properties: | ||
| {% if method.responseModel and spec.definitions[method.responseModel] %} | ||
|
|
||
| | Property | Type | Description | | ||
| |----------|------|-------------| | ||
| {% for property in spec.definitions[method.responseModel].properties -%} | ||
| | `{{ property.name | caseCamel }}` | `{{ property | getPropertyType }}` | {{ property.description | raw }} | | ||
| {% endfor %} | ||
| {%- endif %} | ||
| {% endif %} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: appwrite/sdk-generator
Length of output: 716
🏁 Script executed:
Repository: appwrite/sdk-generator
Length of output: 5968
Add default cases to match expressions to prevent UnhandledMatchError.
Both match expressions in
getParamExample()(lines ~145-150 and ~152-163) lack default cases. If$typecontains an unexpected value, an UnhandledMatchError is thrown. Adddefault => $example,or an appropriate fallback to handle unknown types. The unused parameters$specand$langare required by the parent abstract method signatures and should be kept.🧰 Tools
🪛 PHPMD (2.15.0)
75-75: Avoid unused parameters such as '$spec'. (undefined)
(UnusedFormalParameter)
🤖 Prompt for AI Agents