Skip to content

Commit

Permalink
cache OpenApi schema generation
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Oct 22, 2024
1 parent a619704 commit e5da4d4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-walls-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/platform": patch
---

cache OpenApi schema generation
8 changes: 8 additions & 0 deletions packages/platform/src/OpenApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @since 1.0.0
*/
import * as Context from "effect/Context"
import { globalValue } from "effect/GlobalValue"
import * as HashSet from "effect/HashSet"
import * as Option from "effect/Option"
import type { ReadonlyRecord } from "effect/Record"
Expand Down Expand Up @@ -117,11 +118,16 @@ export interface Annotatable {
readonly annotations: Context.Context<never>
}

const apiCache = globalValue("@effect/platform/OpenApi/apiCache", () => new WeakMap<HttpApi.HttpApi.Any, OpenAPISpec>())

/**
* @category constructors
* @since 1.0.0
*/
export const fromApi = <A extends HttpApi.HttpApi.Any>(self: A): OpenAPISpec => {
if (apiCache.has(self)) {
return apiCache.get(self)!
}
const api = self as unknown as HttpApi.HttpApi.AnyWithProps
const jsonSchemaDefs: Record<string, JsonSchema.JsonSchema> = {}
const spec: DeepMutable<OpenAPISpec> = {
Expand Down Expand Up @@ -334,6 +340,8 @@ export const fromApi = <A extends HttpApi.HttpApi.Any>(self: A): OpenAPISpec =>
}
})

apiCache.set(self, spec)

return spec
}

Expand Down

0 comments on commit e5da4d4

Please sign in to comment.