Skip to content

Commit

Permalink
feat(openapi): move OpenAPI support to 3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
nfroidure committed May 29, 2024
1 parent 7a13fc7 commit 178f866
Show file tree
Hide file tree
Showing 54 changed files with 663 additions and 648 deletions.
4 changes: 2 additions & 2 deletions packages/whook-aws-lambda/src/commands/testHTTPLambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type {
} from '@whook/whook';
import type { AppEnvVars } from 'application-services';
import type { LogService, TimeService } from 'common-services';
import type { OpenAPIV3 } from 'openapi-types';
import type { OpenAPIV3_1 } from 'openapi-types';
import type { WhookAPIOperationAWSLambdaConfig } from '../index.js';
import type { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';

Expand Down Expand Up @@ -69,7 +69,7 @@ async function initTestHTTPLambdaCommand({
APP_ENV: string;
PROJECT_DIR: string;
COMPILER_OPTIONS?: WhookCompilerOptions;
API: OpenAPIV3.Document;
API: OpenAPIV3_1.Document;
time: TimeService;
log: LogService;
args: WhookCommandArgs;
Expand Down
4 changes: 2 additions & 2 deletions packages/whook-aws-lambda/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import type {
WhookCompilerOptions,
WhookCompilerService,
} from '@whook/whook';
import type { OpenAPIV3 } from 'openapi-types';
import type { OpenAPIV3_1 } from 'openapi-types';
import type { LogService } from 'common-services';
import type { CprOptions } from 'cpr';

Expand Down Expand Up @@ -171,7 +171,7 @@ export async function runBuild(
compiler: WhookCompilerService;
log: LogService;
$autoload: Autoloader<Initializer<Dependencies, Service>>;
API: OpenAPIV3.Document;
API: OpenAPIV3_1.Document;
buildInitializer: BuildInitializer;
} = await $.run([
'APP_ENV',
Expand Down
14 changes: 8 additions & 6 deletions packages/whook-aws-lambda/src/services/_autoload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import type {
ServiceInitializerWrapper,
} from 'knifecycle';
import type { LogService } from 'common-services';
import type { OpenAPIV3 } from 'openapi-types';
import type { OpenAPIV3_1 } from 'openapi-types';
import type {
WhookAPIOperationAWSLambdaConfig,
WhookAWSLambdaConfiguration,
Expand Down Expand Up @@ -99,12 +99,12 @@ const initializerWrapper: ServiceInitializerWrapper<
path: string;
}>
> => {
let API: OpenAPIV3.Document;
let API: OpenAPIV3_1.Document;
let OPERATION_APIS: WhookRawOperation<WhookAPIOperationAWSLambdaConfig>[];
const getAPIOperation: (
serviceName: string,
) => Promise<
[WhookAWSLambdaConfiguration['type'], string, OpenAPIV3.Document]
[WhookAWSLambdaConfiguration['type'], string, OpenAPIV3_1.Document]
> = (() => {
return async (serviceName) => {
const cleanedName = serviceName.split('_').pop();
Expand All @@ -131,11 +131,11 @@ const initializerWrapper: ServiceInitializerWrapper<
}

// eslint-disable-next-line
const OPERATION_API: OpenAPIV3.Document = cleanupOpenAPI({
const OPERATION_API: OpenAPIV3_1.Document = cleanupOpenAPI({
...API,
paths: {
[OPERATION.path]: {
[OPERATION.method]: API.paths[OPERATION.path]?.[OPERATION.method],
[OPERATION.method]: API.paths?.[OPERATION.path]?.[OPERATION.method],
},
},
});
Expand All @@ -152,7 +152,9 @@ const initializerWrapper: ServiceInitializerWrapper<
{
path: OPERATION.path,
method: OPERATION.method,
...OPERATION_API.paths[OPERATION.path]?.[OPERATION.method],
...OPERATION_API.paths?.[OPERATION.path]?.[
OPERATION.method
],
parameters: OPERATION.parameters,
},
])
Expand Down
10 changes: 5 additions & 5 deletions packages/whook-aws-lambda/src/wrappers/awsConsumerLambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
WhookWrapper,
} from '@whook/whook';
import type { TimeService, LogService } from 'common-services';
import type { OpenAPIV3 } from 'openapi-types';
import type { OpenAPIV3_1 } from 'openapi-types';
import type {
KinesisStreamEvent,
SQSEvent,
Expand Down Expand Up @@ -47,7 +47,7 @@ export type LambdaConsumerOutput = WhookResponse<number, WhookHeaders, void>;

export type WhookWrapConsumerLambdaDependencies = {
ENV: AppEnvVars;
OPERATION_API: OpenAPIV3.Document;
OPERATION_API: OpenAPIV3_1.Document;
apm: WhookAPMService;
time?: TimeService;
log?: LogService;
Expand Down Expand Up @@ -111,12 +111,12 @@ async function handleForAWSConsumerLambda(
context: Context,
callback: (err: Error) => void,
) {
const path = Object.keys(OPERATION_API.paths)[0];
const method = Object.keys(OPERATION_API.paths[path] || {})[0];
const path = Object.keys(OPERATION_API.paths || {})?.[0];
const method = Object.keys(OPERATION_API.paths?.[path] || {})[0];
const OPERATION: WhookOperation = {
path,
method,
...OPERATION_API.paths[path]?.[method],
...OPERATION_API.paths?.[path]?.[method],
};
const startTime = time();
const parameters: LambdaConsumerInput = {
Expand Down
10 changes: 5 additions & 5 deletions packages/whook-aws-lambda/src/wrappers/awsCronLambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
WhookWrapper,
} from '@whook/whook';
import type { LogService, TimeService } from 'common-services';
import type { OpenAPIV3 } from 'openapi-types';
import type { OpenAPIV3_1 } from 'openapi-types';
import type { ScheduledEvent, Context } from 'aws-lambda';
import type { JsonObject } from 'type-fest';
import type { AppEnvVars } from 'application-services';
Expand All @@ -24,7 +24,7 @@ export type LambdaCronOutput = WhookResponse<number, WhookHeaders, void>;

export type WhookWrapCronLambdaDependencies = {
ENV: AppEnvVars;
OPERATION_API: OpenAPIV3.Document;
OPERATION_API: OpenAPIV3_1.Document;
apm: WhookAPMService;
time?: TimeService;
log?: LogService;
Expand Down Expand Up @@ -83,12 +83,12 @@ async function handleForAWSCronLambda<T extends JsonObject = JsonObject>(
context: Context,
callback: (err: Error) => void,
) {
const path = Object.keys(OPERATION_API.paths)[0];
const method = Object.keys(OPERATION_API.paths[path] || {})[0];
const path = Object.keys(OPERATION_API.paths || {})[0];
const method = Object.keys(OPERATION_API.paths?.[path] || {})[0];
const OPERATION: WhookOperation = {
path,
method,
...OPERATION_API.paths[path]?.[method],
...OPERATION_API.paths?.[path]?.[method],
};
const startTime = time();
const parameters: LambdaCronInput<T> = {
Expand Down
16 changes: 8 additions & 8 deletions packages/whook-aws-lambda/src/wrappers/awsHTTPLambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ import type {
WhookWrapper,
} from '@whook/whook';
import type { TimeService, LogService } from 'common-services';
import type { OpenAPIV3 } from 'openapi-types';
import type { OpenAPIV3_1 } from 'openapi-types';
import type { Readable } from 'stream';
import {
DereferencedParameterObject,
type DereferencedParameterObject,
pickAllHeaderValues,
} from '@whook/http-transaction';
import type {
Expand All @@ -65,7 +65,7 @@ const uuidPattern =
'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$';

export type WhookWrapConsumerLambdaDependencies = {
OPERATION_API: OpenAPIV3.Document;
OPERATION_API: OpenAPIV3_1.Document;
ENV: AppEnvVars;
DEBUG_NODE_ENVS?: string[];
DECODERS?: typeof DEFAULT_DECODERS;
Expand Down Expand Up @@ -135,8 +135,8 @@ async function initWrapHandlerForConsumerLambda<S extends WhookHandler>({
}: WhookWrapConsumerLambdaDependencies): Promise<WhookWrapper<S>> {
log('debug', '📥 - Initializing the AWS LAmbda consumer wrapper.');

const path = Object.keys(OPERATION_API.paths)[0];
const pathObject = OPERATION_API.paths[path];
const path = Object.keys(OPERATION_API.paths || {})[0];
const pathObject = OPERATION_API.paths?.[path];

if (typeof pathObject === 'undefined' || '$ref' in pathObject) {
throw new YError('E_BAD_OPERATION', 'pathObject', pathObject);
Expand Down Expand Up @@ -175,7 +175,7 @@ async function initWrapHandlerForConsumerLambda<S extends WhookHandler>({
const validators = prepareParametersValidators(
ajv,
operation.operationId,
((operation.parameters || []) as OpenAPIV3.ParameterObject[]).concat(
((operation.parameters || []) as DereferencedParameterObject[]).concat(
ammendedParameters,
),
);
Expand Down Expand Up @@ -371,13 +371,13 @@ async function handleForAWSHTTPLambda(
// specified and it is not a binary one
const responseObject =
operation.responses &&
(operation.responses[response.status] as OpenAPIV3.ResponseObject);
(operation.responses[response.status] as OpenAPIV3_1.ResponseObject);
const responseSchema =
responseObject &&
responseObject.content &&
responseObject.content[response.headers['content-type']] &&
(responseObject.content[response.headers['content-type']]
.schema as OpenAPIV3.SchemaObject);
.schema as OpenAPIV3_1.SchemaObject);
const responseHasSchema =
responseSchema &&
(responseSchema.type !== 'string' || responseSchema.format !== 'binary');
Expand Down
10 changes: 5 additions & 5 deletions packages/whook-aws-lambda/src/wrappers/awsKafkaConsumerLambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
WhookWrapper,
} from '@whook/whook';
import type { TimeService, LogService } from 'common-services';
import type { OpenAPIV3 } from 'openapi-types';
import type { OpenAPIV3_1 } from 'openapi-types';
import type { MSKEvent, Context } from 'aws-lambda';
import type { AppEnvVars } from 'application-services';

Expand All @@ -24,7 +24,7 @@ export type LambdaKafkaConsumerOutput = WhookResponse<

export type WhookWrapKafkaLambdaDependencies = {
ENV: AppEnvVars;
OPERATION_API: OpenAPIV3.Document;
OPERATION_API: OpenAPIV3_1.Document;
apm: WhookAPMService;
time?: TimeService;
log?: LogService;
Expand Down Expand Up @@ -83,12 +83,12 @@ async function handleForAWSKafkaConsumerLambda(
context: Context,
callback: (err: Error) => void,
) {
const path = Object.keys(OPERATION_API.paths)[0];
const method = Object.keys(OPERATION_API.paths[path] || {})[0];
const path = Object.keys(OPERATION_API.paths || {})[0];
const method = Object.keys(OPERATION_API.paths?.[path] || {})[0];
const OPERATION: WhookOperation = {
path,
method,
...OPERATION_API.paths[path]?.[method],
...OPERATION_API.paths?.[path]?.[method],
};
const startTime = time();
const parameters: LambdaKafkaConsumerInput = {
Expand Down
10 changes: 5 additions & 5 deletions packages/whook-aws-lambda/src/wrappers/awsLogSubscriberLambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
WhookWrapper,
} from '@whook/whook';
import type { TimeService, LogService } from 'common-services';
import type { OpenAPIV3 } from 'openapi-types';
import type { OpenAPIV3_1 } from 'openapi-types';
import type { JsonValue } from 'type-fest';
import type {
CloudWatchLogsEvent,
Expand All @@ -33,7 +33,7 @@ export type LambdaLogSubscriberOutput = WhookResponse<

export type WhookWrapLogSubscriberLambdaDependencies = {
ENV: AppEnvVars;
OPERATION_API: OpenAPIV3.Document;
OPERATION_API: OpenAPIV3_1.Document;
apm: WhookAPMService;
time?: TimeService;
log?: LogService;
Expand Down Expand Up @@ -97,12 +97,12 @@ async function handleForAWSLogSubscriberLambda<
context: Context,
callback: (err: Error) => void,
) {
const path = Object.keys(OPERATION_API.paths)[0];
const method = Object.keys(OPERATION_API.paths[path] || {})[0];
const path = Object.keys(OPERATION_API.paths || {})[0];
const method = Object.keys(OPERATION_API.paths?.[path] || {})[0];
const OPERATION: WhookOperation = {
path,
method,
...OPERATION_API.paths[path]?.[method],
...OPERATION_API.paths?.[path]?.[method],
};
const startTime = time();
const parameters = {
Expand Down
10 changes: 5 additions & 5 deletions packages/whook-aws-lambda/src/wrappers/awsS3Lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
WhookWrapper,
} from '@whook/whook';
import type { LogService, TimeService } from 'common-services';
import type { OpenAPIV3 } from 'openapi-types';
import type { OpenAPIV3_1 } from 'openapi-types';
import type { S3Event, Context } from 'aws-lambda';
import type { AppEnvVars } from 'application-services';

Expand All @@ -20,7 +20,7 @@ export type LambdaS3Output = WhookResponse<number, WhookHeaders, void>;

export type WhookWrapS3LambdaDependencies = {
ENV: AppEnvVars;
OPERATION_API: OpenAPIV3.Document;
OPERATION_API: OpenAPIV3_1.Document;
apm: WhookAPMService;
time?: TimeService;
log?: LogService;
Expand Down Expand Up @@ -79,12 +79,12 @@ async function handleForAWSS3Lambda(
context: Context,
callback: (err: Error) => void,
) {
const path = Object.keys(OPERATION_API.paths)[0];
const method = Object.keys(OPERATION_API.paths[path] || {})[0];
const path = Object.keys(OPERATION_API.paths || {})[0];
const method = Object.keys(OPERATION_API.paths?.[path] || {})[0];
const OPERATION: WhookOperation = {
path,
method,
...OPERATION_API.paths[path]?.[method],
...OPERATION_API.paths?.[path]?.[method],
};
const startTime = time();
const parameters = {
Expand Down
12 changes: 6 additions & 6 deletions packages/whook-aws-lambda/src/wrappers/awsTransformerLambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
WhookWrapper,
} from '@whook/whook';
import type { TimeService, LogService } from 'common-services';
import type { OpenAPIV3 } from 'openapi-types';
import type { OpenAPIV3_1 } from 'openapi-types';
import type {
FirehoseTransformationEvent,
FirehoseTransformationEventRecord,
Expand All @@ -23,7 +23,7 @@ import type { AppEnvVars } from 'application-services';

type TransformerWrapperDependencies = {
ENV: AppEnvVars;
OPERATION_API: OpenAPIV3.Document;
OPERATION_API: OpenAPIV3_1.Document;
apm: WhookAPMService;
time?: TimeService;
log?: LogService;
Expand Down Expand Up @@ -57,7 +57,7 @@ export type LambdaTransformerOutput = WhookResponse<

export type WhookWrapConsumerLambdaDependencies = {
ENV: AppEnvVars;
OPERATION_API: OpenAPIV3.Document;
OPERATION_API: OpenAPIV3_1.Document;
apm: WhookAPMService;
time?: TimeService;
log?: LogService;
Expand Down Expand Up @@ -119,12 +119,12 @@ async function handleForAWSTransformerLambda(
context: Context,
callback: (err: Error | null, result?: FirehoseTransformationResult) => void,
) {
const path = Object.keys(OPERATION_API.paths)[0];
const method = Object.keys(OPERATION_API.paths[path] || {})[0];
const path = Object.keys(OPERATION_API.paths || {})[0];
const method = Object.keys(OPERATION_API.paths?.[path] || {})[0];
const OPERATION: WhookOperation = {
path,
method,
...OPERATION_API.paths[path]?.[method],
...OPERATION_API.paths?.[path]?.[method],
};
const startTime = time();
const parameters = {
Expand Down
2 changes: 1 addition & 1 deletion packages/whook-cors/src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ exports[`augmentAPIWithCORS() should work 1`] = `
"title": "Sample OpenAPI",
"version": "1.0.0",
},
"openapi": "3.0.2",
"openapi": "3.1.0",
"paths": {
"/crons/tokens": {
"post": {
Expand Down
8 changes: 4 additions & 4 deletions packages/whook-cors/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { handler } from 'knifecycle';
import { YHTTPError } from 'yhttperror';
import type { CORSConfig } from './index.js';
import type { OpenAPIV3 } from 'openapi-types';
import type { OpenAPIV3_1 } from 'openapi-types';
import type { WhookOperation } from '@whook/whook';
import type { LogService } from 'common-services';

Expand Down Expand Up @@ -216,7 +216,7 @@ describe('augmentAPIWithCORS()', () => {
it('should work', async () => {
expect(
await augmentAPIWithCORS({
openapi: '3.0.2',
openapi: '3.1.0',
info: {
version: '1.0.0',
title: 'Sample OpenAPI',
Expand Down Expand Up @@ -354,8 +354,8 @@ describe('augmentAPIWithCORS()', () => {
description: 'Pong',
},
},
} as OpenAPIV3.OperationObject,
},
},
} as OpenAPIV3_1.PathItemObject,
},
}),
).toMatchSnapshot();
Expand Down
Loading

0 comments on commit 178f866

Please sign in to comment.