farrow-js/farrow

Optimize Request: farrow-api-client codegen

Closed this issue · 3 comments

For now, the generated code is like this:

import { apiPipeline } from 'farrow-api-client'

...  // type declaration

export const url = '/api'

export const api = {
  /**
   * @remarks get note content
   */
  getNote: (input: GetNoteInput) =>
    apiPipeline.invoke(url, { path: ['getNote'], input }) as Promise<
      GetNoteSuccess | NoteNotExist | NoteIsBroken | SystemError
    >,

  ... // anthor apis
}

in this segment, the url is constant but need pass to api.invoke everytime. IMO, I prefer:

import { createApiPipeline } from 'farrow-api-client'

...  // type declaration

export const url = '/api'
export const apiPipeline = createApiPipeline(url)

export const api = {
  /**
   * @remarks get note content
   */
  getNote: (input: GetNoteInput) =>
    apiPipeline.invoke({ path: ['getNote'], input }) as Promise<
      GetNoteSuccess | NoteNotExist | NoteIsBroken | SystemError
    >,

  ... // anthor apis
}

reason:

  • generated code splited to file by services
  • services defined in farrow.config.js and url is independent in each service
  • one url -> one service -> one file generated -> one api created by createApiPipeline

It looks good!

I will refactor farrow-api-* as you suggest.

farrow v1.8.5 was released with this feature. Now each api has its own apiPipeline, and all api share one root apiPipeline.