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)
})
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)
})