Skip to content

Add Deno Response Models #291

@TorstenDittmann

Description

@TorstenDittmann

This issue will cover on how to add response models for Deno.

The twig templates for Deno can be found here => https://github.com/appwrite/sdk-generator/tree/master/templates/deno

For reference - you can use this PR which has implemented this for Web => #261

Following things are important:

  • We can have all Models in a single file - which can be imported everywhere. (for example src/models.ts)
  • Add generics to Models that have additionalProperties in their spec (like Document)
  • Pass a generic to Models that are nested and have additionalProperties (like DocumentList that has Document nested)
  • Make sure arrays are handled properly (The documents attribute on DocumentList uses Document[])

This is how we generate the typings on Web SDK:

{% macro sub_schema(property) %}{% if property.sub_schema %}{% if property.type == 'array' %}{{property.sub_schema | caseUcfirst}}[]{% else %}{{property.sub_schema | caseUcfirst}}{% endif %}{% else %}{{property.type | typeName}}{% endif %}{% endmacro %}

namespace Models {
{% for definition in spec.definitions %}
    /**
     * {{ definition.description }}
     */
    export type {{ definition.name | caseUcfirst }}<{% for property in definition.properties %}{% if spec.definitions[property.sub_schema].additionalProperties %}{{property.sub_schema | caseUcfirst}} extends Models.{{property.sub_schema | caseUcfirst}}{% endif %}{% endfor %}> = {
{% for property in definition.properties %}
        /**
         * {{ property.description }}
         */
        {{ property.name | escapeKeyword }}{% if not property.required %}?{% endif %}: {{_self.sub_schema(property)}};
{% endfor %}
    }
{% endfor %}
}

What we want to achieve

This is an example of how the before and after should look like.

Before

async getCurrencies<T extends unknown>(): Promise<T> {
    // ...
}

After

namespace Models {
    /**
     * Currencies List
     */
    export type CurrencyList<> = {
        /**
         * Total sum of items in the list.
         */
        sum: number;
        /**
         * List of currencies.
         */
        currencies: Currency[];
    }
    /**
     * Currency
     */
    export type Currency<> = {
        /**
         * Currency symbol.
         */
        symbol: string;
        /**
         * Currency name.
         */
        name: string;
        /**
         * Currency native symbol.
         */
        symbolNative: string;
        /**
         * Number of decimal digits.
         */
        decimalDigits: number;
        /**
         * Currency digit rounding.
         */
        rounding: number;
        /**
         * Currency code in [ISO 4217-1](http://en.wikipedia.org/wiki/ISO_4217) three-character format.
         */
        code: string;
        /**
         * Currency plural name
         */
        namePlural: string;
    }
}

async getCurrencies(collectionId: string, documentId: string): Promise<Models.CurrencyList> {
    // ...
}

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions