zardoy / public-roadmap

My pulic TODO list on GitHub
1 stars 0 forks source link

GotTypes (15.08.2021) #2

Open zardoy opened 3 years ago

zardoy commented 3 years ago

It recursively finds got (or other fetcher) and tries to fetch JSON, generate types from JSON and place them right before got calls.

For example:

// src/index.ts
import got from 'got'

const post = got('https://jsonplaceholder.typicode.com/posts/1', {
    // response type MUST be json
    responseType: 'json'
})

console.log(post.title)

After running got-types:

// src/__generated__/post.ts
// can be Post Get ...
export interface GetPostType {
    userId: number;
    id:     number;
    title:  string;
    body:   string;
}
// src/index.ts
import got from 'got'
import { GetPostType } from './__generated__/post.ts'

const post = got<GetPostType>('https://jsonplaceholder.typicode.com/posts/1', {
    // response type MUST be json
    responseType: 'json'
})

console.log(post.title)