farrow icon indicating copy to clipboard operation
farrow copied to clipboard

[IDEA] `farrow instrospect` instrospect the api since it has already known args type and response type

Open xialvjun opened this issue 4 years ago • 1 comments

http.match({
  url: '/product/<id:number>?<a:number>&<b:string>&<c:boolean>',
  method: 'POST',
  body: {
    a: Number,
    b: String,
    c: Boolean,
  },
  headers: {
    a: Number,
    b: String,
    c: Boolean,
  },
  cookies: {
    a: Number,
    b: String,
    c: Boolean,
  },
}).use(async (req, next) => {
  return Response.json({ name: 'xialvjun', age: 29 });
})

then use typescript compiler api, farrow instrospect generate json file:

{
  apis: [{
    url: '/product/<id:number>?<a:number>&<b:string>&<c:boolean>',
    method: 'POST',
    body: {
      a: 'number',
      b: 'string',
      c: 'boolean',
    },
    headers: {
      a: 'number',
      b: 'string',
      c: 'boolean',
    },
    cookies: {
      a: 'number',
      b: 'string',
      c: 'boolean',
    },
    response: {
      name: 'string',
      age: 'number'
    }
  }],
}

then farrow generate --plugin=fetch generate js client code:

export const productById = (id: number, query: {a:number,b:string,c:boolean}, xxxx): Promise<{name:string,age:number}> => {xxxx}

I know, this is a huge workload.

Eg the match schema body: {a:Number,b:String,c:Boolean} with constructor is naive. yup is much better which can also give you the validated request type.

I just came up with this idea. I don't really need it since I use graphql.

So I'm not going to contribute on this, I'm trying to translate apollo-koa-server to apollo-farrow-server. I know there is 'farrow-graphql', but 'apollo' and 'graphql-code-generator' works grate.

xialvjun avatar Jan 08 '21 13:01 xialvjun

apollo-farrow-server seems good:)

Lucifier129 avatar Jan 08 '21 15:01 Lucifier129