Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing bearer format field #3813

Merged
merged 3 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nervous-foxes-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/platform": patch
---

Add support for bearer format OpenApi annotation
86 changes: 46 additions & 40 deletions packages/platform/src/OpenApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,64 +58,65 @@ export class ExternalDocs
* @category annotations
*/
export class Servers
extends Context.Tag("@effect/platform/OpenApi/Servers")<ExternalDocs, ReadonlyArray<OpenAPISpecServer>>()
extends Context.Tag("@effect/platform/OpenApi/Servers")<Servers, ReadonlyArray<OpenAPISpecServer>>()
{}

/**
* @since 1.0.0
* @category annotations
*/
export class Override extends Context.Tag("@effect/platform/OpenApi/Override")<Override, Record<string, unknown>>() {}
export class Format extends Context.Tag("@effect/platform/OpenApi/Format")<Format, string>() {}

/**
* @since 1.0.0
* @category annotations
*/
export const annotations = (annotations: {
readonly identifier?: string | undefined
readonly title?: string | undefined
readonly description?: string | undefined
readonly version?: string | undefined
readonly license?: OpenAPISpecLicense | undefined
readonly externalDocs?: OpenAPISpecExternalDocs | undefined
readonly servers?: ReadonlyArray<OpenAPISpecServer> | undefined
readonly override?: Record<string, unknown> | undefined
}): Context.Context<never> => {
let context = Context.empty()
if (annotations.identifier !== undefined) {
context = Context.add(context, Identifier, annotations.identifier)
}
if (annotations.title !== undefined) {
context = Context.add(context, Title, annotations.title)
}
if (annotations.description !== undefined) {
context = Context.add(context, Description, annotations.description)
}
if (annotations.version !== undefined) {
context = Context.add(context, Version, annotations.version)
}
if (annotations.license !== undefined) {
context = Context.add(context, License, annotations.license)
}
if (annotations.externalDocs !== undefined) {
context = Context.add(context, ExternalDocs, annotations.externalDocs)
}
if (annotations.servers !== undefined) {
context = Context.add(context, Servers, annotations.servers)
export class Override extends Context.Tag("@effect/platform/OpenApi/Override")<Override, Record<string, unknown>>() {}

const contextPartial = <Tags extends Record<string, Context.Tag<any, any>>>(tags: Tags): (
options: {
readonly [K in keyof Tags]?: Context.Tag.Service<Tags[K]> | undefined
}
if (annotations.override !== undefined) {
context = Context.add(context, Override, annotations.override)
) => Context.Context<never> => {
const entries = Object.entries(tags)
return (options) => {
let context = Context.empty()
for (const [key, tag] of entries) {
if (options[key] !== undefined) {
context = Context.add(context, tag, options[key]!)
}
}
return context
}
return context
}

/**
* @since 1.0.0
* @category annotations
*/
export interface Annotatable {
readonly annotations: Context.Context<never>
}
export const annotations: (
options: {
readonly identifier?: string | undefined
readonly title?: string | undefined
readonly version?: string | undefined
readonly description?: string | undefined
readonly license?: OpenAPISpecLicense | undefined
readonly externalDocs?: OpenAPISpecExternalDocs | undefined
readonly servers?: ReadonlyArray<OpenAPISpecServer> | undefined
readonly format?: string | undefined
readonly override?: Record<string, unknown> | undefined
}
) => Context.Context<never> = contextPartial({
identifier: Identifier,
title: Title,
version: Version,
description: Description,
license: License,
externalDocs: ExternalDocs,
servers: Servers,
format: Format,
override: Override
})

/**
* @category constructors
Expand Down Expand Up @@ -351,10 +352,15 @@ const makeSecurityScheme = (security: HttpApiSecurity): OpenAPISecurityScheme =>
}
}
case "Bearer": {
const format = Context.getOption(security.annotations, Format).pipe(
Option.map((format) => ({ bearerFormat: format })),
Option.getOrUndefined
)
return {
...meta,
type: "http",
scheme: "bearer"
scheme: "bearer",
...format
}
}
case "ApiKey": {
Expand Down