Creating a composable schema for your HTTP client involves defining TypeScript types for the succ...

Hi, i am creating cutom http client for a backend endpoint, which has defined structure of success response and error response which are

// for success
{ status: number; data:? any; message?: string }


// for error
{ error: { status: number; message: string; code: string; data?: any; debug?: any } }


how can i make these schema composable for data property, since i want to pass schema for the expected data from endpoint. same for error schema, data can be based on the error code, example, code E_VALIDATION_ERROR will have data property.

now i am creating custom http client,

class DynoHttpClient extends Effect.Service<DynoHttpClient>()('DynoHttpClient', {
  dependencies: [FetchHttpClient.layer],
  effect: Effect.gen(function* () {
    const defaultClient = yield * HttpClient.HttpClient
    return defaultClient.pipe(
      HttpClient.mapRequest(request => request.pipe(
        HttpClientRequest.prependUrl('http://localhost:3000/api/_dyno'),
        HttpClientRequest.setHeader('Content-Type', 'application/json'),
        HttpClientRequest.setHeader('x-api-key', 'my-api-key'),
      )),
      HttpClient.transformResponse(response => response.pipe(
        // what transformation should i apply here?
      )),
    )
  }),
}) {}


i want to transform my response based on the response body and not status, if body has success schema structure then it should succeed with success response schema, else if it has error schema structure then it should fail with error response schema, if both are not valid, then it should fail with MalformedServerResponseError.

can anyone guide me through this?
Was this page helpful?