Skip to content

Commit

Permalink
add generic types, ResourceType and SafeAccess
Browse files Browse the repository at this point in the history
  • Loading branch information
kainpets committed Jul 1, 2024
1 parent 9fa4604 commit 4d6be94
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/lib/blueprint.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
import type { Openapi } from './openapi.js'

export interface Blueprint {
export interface Blueprint<T extends TypesModule> {
name: string
routes: Route[]
resources: Record<ResourceType, Resource>
routes: Array<Route<T>>
resources: Partial<Record<ResourceType<T>, Resource<T>>>
}

interface Route {
interface Route<T extends TypesModule> {
path: string
namespace: Namespace | null
endpoints: Endpoint[]
subroutes: Route[]
endpoints: Array<Endpoint<T>>
subroutes: Array<Route<T>>
}

interface Resource {
resourceType: ResourceType
interface Resource<T extends TypesModule> {
resourceType: ResourceType<T>
properties: Property[]
}

// Helper type to safely access nested properties of openapi schema - defualt to unknown if not found
type SafeAccess<T, K extends string> = K extends keyof T ? T[K] : unknown

type ResourceType<T extends TypesModule> = keyof SafeAccess<
SafeAccess<T['openapi'], 'components'>,
'schemas'
>

interface Namespace {
path: string
}

interface Endpoint {
interface Endpoint<T extends TypesModule> {
name: string
path: string
methods: Method[]
Expand All @@ -34,7 +42,7 @@ interface Endpoint {
deprecationMessage: string
parameters: Parameter[]
request: Request
response: Response
response: Response<T>
}

interface Parameter {
Expand All @@ -53,11 +61,11 @@ interface Request {
parameters: Parameter[]
}

interface Response {
interface Response<T extends TypesModule> {
description: string
responseType: 'resource' | 'resource_list' | 'void'
responseKey: string | null
resourceType: ResourceType | null
resourceType: ResourceType<T>
}

interface Property {
Expand All @@ -66,15 +74,15 @@ interface Property {
properties: Property[] | null
}

type ResourceType = 'access_code' | 'user' | 'order' | string

type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'

export interface TypesModule {
openapi: Openapi
}

export const createBlueprint = ({ openapi }: TypesModule): Blueprint => {
export const createBlueprint = <T extends TypesModule>({
openapi,
}: T): Blueprint<T> => {
return {
name: openapi.info.title,
routes: [],
Expand Down

0 comments on commit 4d6be94

Please sign in to comment.