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

Feat/open api doc #2

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 1 addition & 5 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,7 @@ module.exports = {
},
{
files: ['**/src/**'],
excludedFiles: [
'**/__tests__/**',
'**/*.test.ts?(x)',
'**/__benches__/**',
],
excludedFiles: ['**/__tests__/**', '**/*.test.ts?(x)'],
rules: {
'import/no-extraneous-dependencies': [
'error',
Expand Down
34 changes: 0 additions & 34 deletions .github/workflows/codspeed.yml

This file was deleted.

4 changes: 0 additions & 4 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
"production": ["!{projectRoot}/**/*.test.tsx?"]
},
"targetDefaults": {
"bench": {
"inputs": ["default", "^production"],
"dependsOn": ["^package"]
},
"build": {
"inputs": ["production", "^production"],
"dependsOn": ["^build", "^package"]
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"Typescript"
],
"scripts": {
"bench": "nx run-many --target=bench --all --parallel=4",
"build": "nx run-many --target=build --all --parallel=4",
"deploy": "nx run-many --target=deploy --all --parallel=4",
"deploy-affected": "nx affected --target=deploy",
Expand Down
11 changes: 8 additions & 3 deletions packages/aws-zod-interface-contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"module": "index.js",
"types": "index.d.ts",
"scripts": {
"bench": "pnpm tsx src/__benches__/index.ts",
"clean": "rimraf dist *.tsbuildinfo",
"format-check": "prettier --check . --ignore-path ../../.prettierignore",
"format-fix": "prettier --write . --ignore-path ../../.prettierignore",
Expand All @@ -41,22 +40,24 @@
"watch": "pnpm clean && concurrently 'pnpm:package-* --watch'"
},
"dependencies": {
"@anatine/zod-openapi": "^2.2.1",
"@types/http-errors": "^2.0.1",
"http-errors": "^2.0.0",
"lodash": "^4.17.21",
"openapi-types": "^12.1.3",
"ts-toolbelt": "^9.6.0"
},
"devDependencies": {
"@codspeed/tinybench-plugin": "^2.2.0",
"@types/aws-lambda": "^8.10.125",
"@types/lodash": "^4.14.191",
"@types/node": "^18.16.1",
"@vitest/coverage-c8": "0.30.0",
"axios": "^1.2.2",
"concurrently": "^8.0.0",
"dependency-cruiser": "^13.0.0",
"eslint": "^8.29.0",
"prettier": "^2.8.1",
"rimraf": "^5.0.0",
"tinybench": "^2.3.1",
"ts-node": "^10.9.1",
"tsc-alias": "^1.8.2",
"tsup": "^6.7.0",
Expand All @@ -67,11 +68,15 @@
"zod": "^3.22.4"
},
"peerDependencies": {
"axios": ">=1",
"zod": ">=3"
},
"peerDependenciesMeta": {
"zod": {
"optional": false
},
"axios": {
"optional": true
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import axios from 'axios';
import { z } from 'zod';

import { HttpStatusCodes } from 'types/http';

import { ApiGatewayContract } from '../../ApiGatewayContract';
import { getAxiosRequest } from './axiosRequest';

describe('apiGateway axios request', () => {
const pathParametersSchema = z.object({
userId: z.string(),
pageNumber: z.string(),
});

const queryStringParametersSchema = z.object({
testId: z.string(),
optionalParam: z.string().optional(),
});

const headersSchema = z.object({
myHeader: z.string(),
});

const bodySchema = z.object({
foo: z.string(),
bar: z.array(z.string()).optional(),
});

const outputSchema = z.object({
id: z.string(),
name: z.string(),
});

describe('httpApi, when all parameters are set', () => {
const httpApiContract = new ApiGatewayContract({
id: 'testContract',
path: '/users/{userId}',
method: 'GET',
integrationType: 'httpApi',
pathParametersSchema,
queryStringParametersSchema,
headersSchema,
bodySchema,
outputSchemas: {
[HttpStatusCodes.OK]: outputSchema,
},
});

it('should have the correct axiosRequest', async () => {
await expect(() =>
getAxiosRequest(
httpApiContract,
axios.create({ baseURL: 'http://blob.test' }),
{
pathParameters: {
userId: 'azer',
pageNumber: 'zert',
},
queryStringParameters: {
testId: 'erty',
},
headers: {
myHeader: 'rtyu',
},
body: {
foo: 'tyui',
bar: ['yuio'],
},
},
),
).rejects.toMatchObject({
config: {
url: '/users/azer',
data: '{"foo":"tyui","bar":["yuio"]}',
params: { testId: 'erty' },
},
});
});
});

describe('restApi, when it is instantiated with a subset of schemas', () => {
const restApiContract = new ApiGatewayContract({
id: 'testContract',
path: '/coucou',
method: 'POST',
integrationType: 'httpApi',
});

it('should have the correct axios request ', async () => {
await expect(() =>
getAxiosRequest(
restApiContract,
axios.create({ baseURL: 'http://blob.test' }),
{},
),
).rejects.toMatchObject({
config: {
url: '/coucou',
},
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { AxiosInstance, AxiosResponse } from 'axios';

import { GenericApiGatewayContract } from '../../ApiGatewayContract';
import { OutputType, RequestArguments } from '../../types';
import { getRequestParameters } from '../requestParameters';

export const getAxiosRequest = async <
Contract extends GenericApiGatewayContract,
>(
contract: Contract,
axiosClient: AxiosInstance,
requestArguments: RequestArguments<Contract>,
): Promise<AxiosResponse<OutputType<Contract>['body']>> => {
const { path, method, queryStringParameters, body, headers } =
getRequestParameters<Contract>(contract, requestArguments);

return axiosClient.request({
method,
url: path,
headers,
data: body,
params: queryStringParameters,
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { getAxiosRequest } from './axiosRequest';
Loading