Skip to content

v2.2.0 Beta 1

Pre-release
Pre-release
Compare
Choose a tag to compare
@jhaemin jhaemin released this 15 Jun 07:27
· 196 commits to master since this release

This version introduces some new ways to build One APIs.

Removed deprecated utility types

  • OneAPIData: Replaced with using direct access to the property, T['data']
  • OneAPIPayload: Replaced with using direct access to the property, T['payload']
  • OneAPIError<T>: Replaced with OneApiError combined with union types, OneApiError | 'Other Error' | ...

Removed the universal OneApiAction type

At the beginning of its creation, we standardized the One APIs' interface and they could be expressed in the same, single interface called OneApiAction which is an union of all the One APIs. However, the number of APIs are increasing and we found they can be differed in four different structures.

Request Data Response Data Response Error

For the APIs which provide no response data, the payload data is null.

AuthOptional (#48)

Added this type alongside AuthRequired and AuthOnly. It makes the accessToken field optional in request data and oneApiClient can call them with or without the token. This type is suitable for the APIs that act differently in unauthorized session.

One API Templates

Now you can define actions interface with two types of One API templates.

OneApiActionTemplate<D, P>

This type accepts data and payload types.

Here is an example, suppose there is an action interface like below,

// bookmark-tip.action/interface.ts

export interface Action {
  data: AuthRequired<{
    tipId: number
  }>
  payload: {
    err: OneApiError
    data: {
      isBookmarked: boolean
    }
  }
}

You can rewrite the interface with the new types

export type Action = OneApiActionTemplate<
  AuthRequired<
    tipId: number
  >
  OneApiActionTemplatePayload<
    OneApiError,
    {
      isBookmarked: boolean
    }
  >
>

OneApiActionTemplateWithoutRequestData<P> (#48)

This type accepts only the payload type.

get-notice-catalog.action for example, doesn't require any reqeust data. Currently it is defined by filling the data with undefined. It doesn't make sense and oneApiClient always infers the data as required(or optional) property.

// get-notice-catalog.action/interface.ts

export interface Action {
  data: undefined
  payload: {
    err: OneApiError
    data: {
      noticeCatalog: NoticeItem[]
    }
  }
}

But now it can be defined in a much explicit way.

export type Action = OneApiActionTemplateWithoutRequestData<
  OneApiActionTemplatePayload<
    OneApiError,
    {
      noticeCatalog: NoticeItem[]
    }
  >
>

If there is no response data(payload data), pass null to OneApiActionTemplatePayload as the second template type.

export type Action = OneApiActionTemplateWithoutRequestData<
  OneApiActionTemplatePayload<OneApiError, null>
>

In default, you should pass OneApiError as the first template type of OneApiActionTemplatePayload. Use union types if you need additional error types.

Return the response with oneApiResponse (#49)

Previously you're returning the response inside the One API functions like this.

return {
  err: OneApiError.UNAUTHORIZED,
  data: {
    some: 'data'
  }
}

By using the oneApiResponse<Action>(), you can return the response in a much simpler and intuitive way.

Response with only an error

return oneApiResponse<Action>(OneApiError.UNAUTHORIZED)

data will be automatically filled with null

Response with only the data

return oneApiResponse<Action>({
  some: 'data'
})

err will be automatically filled with null

Response with both an error and the data

return oneApiResponse<Action>('Some Error', null)

Don't forget to pass the template type Action. Without doing this, type inferences will be broken.

One API function declaration

Define the One API function with one of these types.

  • OneApiFunc
  • OneApiFuncWithoutRequestData

They accept a template type that extends OneApiActionTemplate and OneApiActionTemplateWithoutRequestData each.

const func: OneApiFunc<Action> = async (data) => {
  const response = oneApiResponse<Action>(OneApiError.UNAUTHORIZED)

  return response
}

OneApiFunction is now deprecated

oneAPIClient is now oneApiClient

In JavaScript, variable naming convention is often using camelCase. And we are trying to keep consistent variable names even if a name is using an abbreviation like API. So we changed oneAPIClient to oneApiClient.