lonestone / nest-sdk-generator Goto Github PK
View Code? Open in Web Editor NEWA REST SDK generator for NestJS. Strictly type your frontend's API calls :rocket:
License: MIT License
A REST SDK generator for NestJS. Strictly type your frontend's API calls :rocket:
License: MIT License
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
).
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 string
s, or ideally recursively parse them directly with new Date(value)
.
https://github.com/samchon/nestia
Your project seems almost same with my project, nestia
.
How about participating in that project or reviewing and advising each other?
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
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.
sdk-generator.json
{
"verbose": true,
"apiInputPath": "../backend",
"sdkOutput": "src/sdk",
"sdkInterfacePath": "src/sdk/sdk-interface.ts"
}
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
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,
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.