@bpinternal/opapi
Advanced tools
Comparing version 0.1.4 to 0.2.0
import { OpenApiZodAny, extendApi } from '@anatine/zod-openapi'; | ||
export { OpenApiZodAny } from '@anatine/zod-openapi'; | ||
import { SchemaObject } from 'openapi3-ts'; | ||
@@ -11,2 +12,26 @@ type SplitPath<P extends string> = P extends `${infer X}/${infer Y}` ? [X, ...SplitPath<Y>] : [P]; | ||
type SchemaType = 'zod-schema' | 'json-schema'; | ||
type SchemaOfType<T extends SchemaType> = T extends 'zod-schema' ? OpenApiZodAny : SchemaObject; | ||
type State<SchemaName extends string, DefaultParameterName extends string, SectionName extends string> = { | ||
metadata: Metadata; | ||
refs: RefMap; | ||
defaultParameters?: { | ||
[name in DefaultParameterName]: Parameter<'json-schema'>; | ||
}; | ||
sections: { | ||
name: SectionName; | ||
title: string; | ||
description: string; | ||
schema?: string; | ||
operations: string[]; | ||
}[]; | ||
schemas: Record<SchemaName, { | ||
schema: SchemaObject; | ||
section: SectionName; | ||
}>; | ||
errors?: ApiError[]; | ||
operations: { | ||
[name: string]: Operation<DefaultParameterName, SectionName, string, 'json-schema'>; | ||
}; | ||
}; | ||
type ApiError = { | ||
@@ -24,2 +49,7 @@ status: 400 | 401 | 402 | 403 | 404 | 405 | 408 | 409 | 413 | 415 | 429 | 500 | 501 | 502 | 503 | 504; | ||
}; | ||
type RefMap = { | ||
[name in ComponentType]: { | ||
[name: string]: boolean; | ||
}; | ||
}; | ||
type BaseParameter = { | ||
@@ -45,26 +75,34 @@ description: string; | ||
}; | ||
type QueryParameterObject = BaseParameter & { | ||
type QueryParameterObject<S extends SchemaType> = BaseParameter & { | ||
type: 'object'; | ||
in: 'query'; | ||
required?: boolean; | ||
schema: OpenApiZodAny; | ||
schema: SchemaOfType<S>; | ||
}; | ||
type Parameter = StandardParameter | PathParameter | QueryParameterObject | QueryParameterStringArray; | ||
type OperationWithBodyProps<DefaultParameterName extends string, SectionName extends string, Path extends string = string> = { | ||
method: 'post' | 'put' | 'patch'; | ||
type Parameter<S extends SchemaType> = StandardParameter | PathParameter | QueryParameterObject<S> | QueryParameterStringArray; | ||
type OperationWithBodyMethod = 'post' | 'put' | 'patch'; | ||
type OperationWithBodyProps<DefaultParameterName extends string, SectionName extends string, Path extends string, S extends SchemaType> = { | ||
method: OperationWithBodyMethod; | ||
requestBody: { | ||
description: string; | ||
schema: OpenApiZodAny; | ||
schema: SchemaOfType<S>; | ||
}; | ||
} & BaseOperationProps<DefaultParameterName, SectionName, Path>; | ||
type OperationWithoutBodyProps<DefaultParameterName extends string, SectionName extends string, Path extends string = string> = { | ||
method: 'get' | 'delete' | 'options' | 'head' | 'trace'; | ||
} & BaseOperationProps<DefaultParameterName, SectionName, Path>; | ||
type Operation<DefaultParameterName extends string, SectionName extends string, Path extends string = string> = OperationWithBodyProps<DefaultParameterName, SectionName, Path> | OperationWithoutBodyProps<DefaultParameterName, SectionName, Path>; | ||
type ParametersMap<Path extends string = string> = Record<PathParams<Path>, PathParameter> & Record<string, Parameter>; | ||
type BaseOperationProps<DefaultParameterName extends string, SectionName extends string, Path extends string> = { | ||
} & BaseOperationProps<DefaultParameterName, SectionName, Path, S>; | ||
type OperationWithoutBodyMethod = 'get' | 'delete' | 'options' | 'head' | 'trace'; | ||
type OperationWithoutBodyProps<DefaultParameterName extends string, SectionName extends string, Path extends string, S extends SchemaType> = { | ||
method: OperationWithoutBodyMethod; | ||
} & BaseOperationProps<DefaultParameterName, SectionName, Path, S>; | ||
type Operation<DefaultParameterName extends string, SectionName extends string, Path extends string, S extends SchemaType> = OperationWithBodyProps<DefaultParameterName, SectionName, Path, S> | OperationWithoutBodyProps<DefaultParameterName, SectionName, Path, S>; | ||
declare enum ComponentType { | ||
SCHEMAS = "schemas", | ||
RESPONSES = "responses", | ||
REQUESTS = "requestBodies", | ||
PARAMETERS = "parameters" | ||
} | ||
type ParametersMap<Path extends string, S extends SchemaType> = Record<PathParams<Path>, PathParameter> & Record<string, Parameter<S>>; | ||
type BaseOperationProps<DefaultParameterName extends string, SectionName extends string, Path extends string, S extends SchemaType> = { | ||
name: string; | ||
path: Path; | ||
description: string; | ||
parameters?: ParametersMap<Path>; | ||
parameters?: ParametersMap<Path, S>; | ||
disableDefaultParameters?: { | ||
@@ -77,13 +115,29 @@ [name in DefaultParameterName]?: boolean; | ||
description: string; | ||
schema: OpenApiZodAny; | ||
schema: SchemaOfType<S>; | ||
}; | ||
}; | ||
type CreateStateProps<SchemaName extends string, DefaultParameterName extends string, SectionName extends string> = { | ||
metadata: Metadata; | ||
defaultParameters?: Record<DefaultParameterName, Parameter<'zod-schema'>>; | ||
schemas?: Record<SchemaName, { | ||
schema: OpenApiZodAny; | ||
section: SectionName; | ||
}>; | ||
sections?: Record<SectionName, { | ||
title: string; | ||
description: string; | ||
}>; | ||
errors?: readonly ApiError[]; | ||
}; | ||
declare function createState<SchemaName extends string, DefaultParameterName extends string, SectionName extends string>(props: CreateStateProps<SchemaName, DefaultParameterName, SectionName>): State<SchemaName, DefaultParameterName, SectionName>; | ||
declare function getRef(state: State<string, string, string>, type: ComponentType, name: string): OpenApiZodAny; | ||
declare const mapParameter: (param: Parameter<'zod-schema'>) => Parameter<'json-schema'>; | ||
declare const schema: typeof extendApi; | ||
type OpenApiProps<SchemaName extends string, DefaultParameterName extends string, SectionName extends string, SchemaSectionName extends SectionName> = { | ||
type OpenApiProps<SchemaName extends string, DefaultParameterName extends string, SectionName extends string> = { | ||
metadata: Metadata; | ||
defaultParameters?: Record<DefaultParameterName, Parameter>; | ||
defaultParameters?: Record<DefaultParameterName, Parameter<'zod-schema'>>; | ||
schemas?: Record<SchemaName, { | ||
schema: OpenApiZodAny; | ||
section: SchemaSectionName; | ||
section: SectionName; | ||
}>; | ||
@@ -100,10 +154,29 @@ sections?: Record<SectionName, { | ||
}; | ||
type OpenApi<SchemaName extends string = string, DefaultParameterName extends string = string, SectionName extends string = string, SchemaSectionName extends SectionName = SectionName> = ReturnType<typeof OpenApi<SchemaName, DefaultParameterName, SectionName, SchemaSectionName>>; | ||
declare function OpenApi<SchemaName extends string, DefaultParameterName extends string, SectionName extends string, SchemaSectionName extends SectionName>(props: OpenApiProps<SchemaName, DefaultParameterName, SectionName, SchemaSectionName>): { | ||
declare const createOpapiFromState: <SchemaName extends string, DefaultParameterName extends string, SectionName extends string>(state: State<SchemaName, DefaultParameterName, SectionName>) => { | ||
getModelRef: (name: SchemaName) => OpenApiZodAny; | ||
addOperation: <Path extends string>(operationProps: Operation<DefaultParameterName, SectionName, Path>) => void; | ||
addOperation: <Path extends string>(operationProps: Operation<DefaultParameterName, SectionName, Path, "zod-schema">) => void; | ||
exportClient: (dir: string | undefined, openapiGeneratorEndpoint: string, postProcessors?: OpenApiPostProcessors) => Promise<void>; | ||
exportServer: (dir: string | undefined, useExpressTypes: boolean) => Promise<void>; | ||
exportOpenapi: (dir?: string) => void; | ||
exportState: (dir?: string) => void; | ||
}; | ||
type OpenApi<SchemaName extends string = string, DefaultParameterName extends string = string, SectionName extends string = string> = ReturnType<typeof createOpapiFromState<SchemaName, DefaultParameterName, SectionName>>; | ||
declare function OpenApi<SchemaName extends string, DefaultParameterName extends string, SectionName extends string>(props: OpenApiProps<SchemaName, DefaultParameterName, SectionName>): { | ||
getModelRef: (name: SchemaName) => OpenApiZodAny; | ||
addOperation: <Path extends string>(operationProps: Operation<DefaultParameterName, SectionName, Path, "zod-schema">) => void; | ||
exportClient: (dir: string | undefined, openapiGeneratorEndpoint: string, postProcessors?: OpenApiPostProcessors | undefined) => Promise<void>; | ||
exportServer: (dir: string | undefined, useExpressTypes: boolean) => Promise<void>; | ||
exportOpenapi: (dir?: string) => void; | ||
exportState: (dir?: string) => void; | ||
}; | ||
declare namespace OpenApi { | ||
const fromState: <SchemaName extends string, DefaultParameterName extends string, SectionName extends string>(state: State<SchemaName, DefaultParameterName, SectionName>) => { | ||
getModelRef: (name: SchemaName) => OpenApiZodAny; | ||
addOperation: <Path extends string>(operationProps: Operation<DefaultParameterName, SectionName, Path, "zod-schema">) => void; | ||
exportClient: (dir: string | undefined, openapiGeneratorEndpoint: string, postProcessors?: OpenApiPostProcessors | undefined) => Promise<void>; | ||
exportServer: (dir: string | undefined, useExpressTypes: boolean) => Promise<void>; | ||
exportOpenapi: (dir?: string) => void; | ||
exportState: (dir?: string) => void; | ||
}; | ||
} | ||
type SchemaOf<O extends OpenApi<any, any, any>> = O extends OpenApi<infer Skema, infer _Param, infer _Sexion> ? Skema : never; | ||
@@ -113,2 +186,2 @@ type ParameterOf<O extends OpenApi<any, any, any>> = O extends OpenApi<infer _Skema, infer Param, infer _Sexion> ? Param : never; | ||
export { CodePostProcessor, OpenApi, OpenApiPostProcessors, OpenApiProps, Operation, Parameter, ParameterOf, ParametersMap, SchemaOf, SectionOf, schema }; | ||
export { ApiError, CodePostProcessor, ComponentType, Metadata, OpenApi, OpenApiPostProcessors, OpenApiProps, Operation, OperationWithBodyMethod, OperationWithBodyProps, OperationWithoutBodyMethod, OperationWithoutBodyProps, Parameter, ParameterOf, ParametersMap, PathParameter, QueryParameterObject, QueryParameterStringArray, SchemaOf, SectionOf, StandardParameter, State, createState, getRef, mapParameter, schema }; |
{ | ||
"name": "@bpinternal/opapi", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"description": "", | ||
@@ -18,6 +18,6 @@ "main": "./dist/index.js", | ||
"build": "tsup src/index.ts --dts --format cjs,esm --clean", | ||
"type-check": "tsc --noEmit" | ||
"type-check": "tsc --noEmit", | ||
"format": "prettier --write ." | ||
}, | ||
"devDependencies": { | ||
"tsup": "^6.7.0", | ||
"@swc/core": "^1.3.23", | ||
@@ -28,4 +28,7 @@ "@swc/helpers": "^0.5.0", | ||
"@types/node": "^18.11.17", | ||
"@types/prettier": "^2.7.3", | ||
"@types/verror": "^1.10.6", | ||
"prettier": "^2.8.8", | ||
"ts-node": "^10.9.1", | ||
"tsup": "^6.7.0", | ||
"typescript": "^4.9.4", | ||
@@ -32,0 +35,0 @@ "vite": "^3.2.5", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 2 instances in 1 package
16467646
2178
1
14
1