Effect CommunityEC
Effect Community8mo ago
2 replies
alphafivezero

Issue with `HttpApiClient` Not Including Headers from Middleware in Endpoint

Hi - I'm encountering an issue with the HttpApiClient with an endpoint using middleware where the security option is configured to look for an api key in the request headers. It appears that the headers specified in Authorization are not included with the endpoint - although the swagger docs correctly show the header as required (repro here: https://effect.website/play/#7024dc41cffb).

class Caller extends Schema.Class<Caller>('Caller')({
  id: Schema.Number,
  name: Schema.String,
}) {}

class CallerContext extends Context.Tag('CurrentUser')<CallerContext, Caller>() {}

export class Authorization extends HttpApiMiddleware.Tag<Authorization>()('Authorization', {
  failure: Unauthorized,
  provides: CallerContext,
  security: {
    apiKey: HttpApiSecurity.apiKey({ in: 'header', key: 'X-API-Key' }),
  },
}) {}

class TasksApi extends HttpApiGroup.make('tasks')
  .add(
    HttpApiEndpoint.get(
      'findById',
    )`/${HttpApiSchema.param('id', Schema.NumberFromString)}`.addSuccess(Task),
  )
  .middleware(Authorization) // <--- MIDDLEWARE ADDED TO ENDPOINT
  .prefix('/tasks')
  .annotateContext(
    OpenApi.annotations({
      description: 'API for managing tasks',
      title: 'Tasks',
    }),
  ) {}

class MyApi extends HttpApi.make('api').add(TasksApi) {}

const program = Effect.gen(function* () {
  const client = yield* HttpApiClient.make(MyApi, {
    baseUrl: 'http://localhost:3000',
  })

  const task = yield* client.tasks.findById({
    headers: {  // <---- TYPESCRIPT COMPLAINS ABOUT THIS
      'X-API-Key': '123',
    },
    path: { id: 1 },
  })

  yield* Effect.logInfo('Task:', task)
})
swagger_shot.png
Was this page helpful?