cool!
Based on Swagger spec 0.0.0
The SDK contains 2 different parts:
- Auto generated code (based on Swagger Spec)
- Specific code dedicated to this SDK
Structure
Auto Generated code
A main part of the SDK is automatically generated from a Swagger Spec. The following folders contain the generated code:
- src/api: Containing the API calls files
- src/models/base: Models based of swagger definitions
The Code can be regenerated by running the following command:
yo @dapi/sdk:core
Where to put my custom code?
There are 2 places where we can add custom code:
- src/helpers: should contain the helper functions to transform the data.
- src/models/custom: should contain the models specific to this SDK (mainly the helper functions return type).
How to extend a Model?
You can extend a base model in 3 steps:
- Redirect the default model to your override:
// src/models/base/<model name>/index.ts
export * from "../../core/<model name>";
- Indicate to Swagger CodeGen that you have override the base model:
# in .swagger-codegen-ignore
src/models/base/<model name>/index.ts
- Create your core models
// src/models/core/<model name>/index.ts
export * from "./<model name>.ts";
export * from "./<model name>.reviver.ts";
// src/models/core/<model name>/<model name>.ts
import { <model name> as Base<model name> } from "../../base/<model name>/<model name>";
export interface <model name> extends BaseB<model name> {
// Additional fields
}
// src/models/core/<model name>/<model name>.reviver.ts
import { yourFunction } from "../../../helpers/<model name>";
import { revive<model name> as Base<model name> } from "../../base/<model name>/<model name>.reviver";
import { <model name> } from "./<model name>";
import {Reviver, utils} from "@dapi/sdk-core";
export function revive<model name><T extends <model name> = <model name>>(data: any, dictionary?: any) {
// TODO: use BaseRevive<T> when ready
const revivedData: T | undefined = Base<model name>(data, dictionary) as T | undefined;
if (!revivedData) { return ; }
if (!revivedData.yourField) {
revivedData.yourField = yourFunction(revivedData);
}
return revivedData;
}
Commands
Some commands are provided to keep your SDK up-to-date.
Generate SDK from a Swagger specification
yo @dapi/sdk:core
Run Unit Tests
You can build and run UT with:
yarn test