Skip to content

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

Open

Description

@xialvjun
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.

Activity

  1. Lucifier129 commented on Jan 8, 2021

    @Lucifier129
    Collaborator

    apollo-farrow-server seems good:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions