GithubHelp home page GithubHelp logo

lonestone / nest-sdk-generator Goto Github PK

View Code? Open in Web Editor NEW
89.0 89.0 10.0 633 KB

A REST SDK generator for NestJS. Strictly type your frontend's API calls :rocket:

License: MIT License

TypeScript 98.92% JavaScript 1.08%

nest-sdk-generator's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

nest-sdk-generator's Issues

Date transformation

Hey, great generator! As a frontend dev it is very convenient to simply import all route typesafe from the backend without having to write all types twice!

However, when trying it out, I noticed a problem with Date: With a controller that has a Date object as type, the generator copies it as such. But Nest.js automatically transforms this to an ISO string, whereby the client type is no longer correct (Date instead of string).

Example

Controller:

// demo.controller.ts

@Controller("demo")
export class DemoController {

    @Get("/route")
    demo() {
        return {
            id: 3,
            date: new Date()
        }
    }

}

Response:

// GET /demo/route
{
  "id": 3,
  "date": "2023-09-24T15:14:48.940Z"
}

Generated SDK:

export default {

    // GET @ /demo/route
    demo(
        params: {} = {},
        body: {} = {},
        query: {} = {},
    ): Promise<{ id: number; date: Date }> {
        return request("GET", `/demo/route`, body, query)
    },
}

Usage:

const { date } = await demoController.demo();

// throws Uncaught TypeError: date.toLocaleDateString is not a function
console.log(date.toLocaleDateString())

Would it be possible (and if so how) to transform these Date objects automatically on the client side? Either change all Date types to strings, or ideally recursively parse them directly with new Date(value).

Support Server-Sent Events

Nest Docu

Server-Sent Events (SSE) (since v7.5.0) are used as follows:

Nest controller

@Sse('sse')
sse(): Observable<MessageEvent> {
  return interval(1000).pipe(map((_) => ({ data: { hello: 'world' } })));
}

Client

const eventSource = new EventSource('/sse');
eventSource.onmessage = ({ data }) => {
  console.log('New message', JSON.parse(data));
};

Full Example from Nest: nestjs/nest/sample/28-sse

Generator fails to use type from library

Tried to use controller, which returns some data directly from stripe library but nest-sdk-generator fails with:

ERROR: File stripe was not found (was expected to contain dependency type Stripe)

image

SDK generator writes wrong path

image

All of paths written in the controller files are omitting the path separator.

For referece, my device is using the Windows.

Failed generation when using @Res() decorator in controllers

Use case

In my application.controller.ts there is such action:

import { Response } from "express";
...
async index(@Res() res: Response) {
...
}

When I run generate command ./node_modules/.bin/nest-sdk-generator ./frontend/sdk-generator.json I get this error:

-> Extracting type Response from file ../node_modules/@types/express/index.d.ts...
ERROR: Type Response was not found in file ../node_modules/@types/express/index.d.ts

When we open @type/express L127 we can see that there is a namespace.

Config

sdk-generator.json

{
    "verbose": true,
    "apiInputPath": "../backend",
    "sdkOutput": "src/sdk",
    "sdkInterfacePath": "src/sdk/sdk-interface.ts"
}

Cause

I think that is because of one of limitation of nest-sdk-generator:

Types belonging to namespaces are currently not supported and will result in error in the generated code

Support for pipes on params

Would it be possible to add support for adding a pipe to a Param decorator?

Currently getting:
Multiple (2) arguments were provided to the decorator
with this code:
@Param('cartId', CartIdPipe) cartId: string,

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.