-
Notifications
You must be signed in to change notification settings - Fork 0
feat(openapi-handler): migrate handler to a dedicated package MAPCO-9762 #212
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
Open
netanelC
wants to merge
7
commits into
master
Choose a base branch
from
create-handler
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
58f2cd4
feat(openapi-helpers): migrate handler to a dedicated package
netanelC 545bb41
style(openapi-helpers): fix lints
netanelC bac2817
chore(openapi-typed-request-handler): release please
netanelC 6d22319
fix(openapi-typed-request-handler): api extractor
netanelC 0de0dcc
fix(openapi-typed-request-handler): add to vscode settings
netanelC aa22b79
fix(openapi-typed-request-handler): pr issues
netanelC 44eb99c
fix(openapi-typed-request-handler): knip
netanelC File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,5 @@ | ||
| { | ||
| "extends": "@map-colonies/tsconfig/tsconfig-library", | ||
| "compilerOptions": { | ||
| "lib": ["ESNext"] | ||
| }, | ||
| "include": ["src", "tests"], | ||
| "exclude": ["dist", "node_modules"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # openapi-typed-request-handler | ||
|
|
||
| Express request handler type utilities for building type-safe APIs based on OpenAPI specifications. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| npm install @map-colonies/openapi-typed-request-handler | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ```typescript | ||
| import { TypedRequestHandlers } from '@map-colonies/openapi-typed-request-handler'; | ||
| import type { paths, operations } from './types.d.ts'; | ||
|
|
||
| // Initialize the TypedRequestHandlers with the paths and operations types | ||
| type MyHandlers = TypedRequestHandlers<paths, operations>; | ||
|
|
||
| export const getUser: MyHandlers['getUserById'] = (req, res) => { | ||
| const id = req.params.id; // Autocomplete! | ||
| res.status(200).json({ id, name: 'John' }); // Type-safe response body! | ||
| }; | ||
|
|
||
| // Or using method/path mapping | ||
| export const getResource: MyHandlers['GET /resource'] = (req, res) => { | ||
| res.status(200).json({ id: 1, name: 'name' }); | ||
| }; | ||
| ``` | ||
4 changes: 2 additions & 2 deletions
4
...penapi-helpers/api-extractor.handler.json → ...-typed-request-handler/api-extractor.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| { | ||
| "$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json", | ||
| "extends": "../../api-extractor.json", | ||
| "mainEntryPointFilePath": "dist/typedRequestHandler/typedRequestHandler.d.ts", | ||
| "mainEntryPointFilePath": "dist/index.d.ts", | ||
| "apiReport": { | ||
| "enabled": true, | ||
| "reportFileName": "handler.<unscopedPackageName>" | ||
| "reportFileName": "openapi-typed-request-handler.api.md" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import baseConfig from '@map-colonies/eslint-config/ts-base'; | ||
| import { defineConfig } from 'eslint/config'; | ||
|
|
||
| export default defineConfig(baseConfig, { ignores: ['vitest.config.ts'] }); |
20 changes: 20 additions & 0 deletions
20
packages/openapi-typed-request-handler/etc/openapi-typed-request-handler.api.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| ## API Report File for "@map-colonies/openapi-typed-request-handler" | ||
|
|
||
| > Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). | ||
|
|
||
| ```ts | ||
|
|
||
| import type { OptionalKeys } from 'ts-essentials'; | ||
| import type { RequestHandler } from 'express'; | ||
|
|
||
| // Warning: (ae-forgotten-export) The symbol "PathsTemplate" needs to be exported by the entry point index.d.ts | ||
| // Warning: (ae-forgotten-export) The symbol "OperationsTemplate" needs to be exported by the entry point index.d.ts | ||
| // Warning: (ae-forgotten-export) The symbol "OperationHandlers" needs to be exported by the entry point index.d.ts | ||
| // Warning: (ae-forgotten-export) The symbol "PathHandlers" needs to be exported by the entry point index.d.ts | ||
| // | ||
| // @public | ||
| export type TypedRequestHandlers<Paths extends PathsTemplate, Operations extends OperationsTemplate> = OperationHandlers<Operations> & PathHandlers<Paths>; | ||
|
|
||
| // (No @packageDocumentation comment for this package) | ||
|
|
||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| { | ||
| "name": "@map-colonies/openapi-typed-request-handler", | ||
| "version": "0.1.0", | ||
| "description": "Express request handler type utilities for OpenAPI-defined APIs", | ||
| "type": "commonjs", | ||
| "exports": { | ||
| ".": { | ||
| "types": "./dist/index.d.ts", | ||
| "default": "./dist/index.js" | ||
| } | ||
| }, | ||
| "scripts": { | ||
| "test": "vitest run", | ||
| "lint": "eslint .", | ||
| "lint:fix": "eslint --fix .", | ||
| "prebuild": "pnpm run clean", | ||
| "build": "tsc --project tsconfig.build.json", | ||
| "clean": "rimraf dist", | ||
| "prepack": "turbo run build", | ||
| "check-dist": "publint && attw --profile node16 --pack .", | ||
| "knip": "knip --directory ../.. --workspace packages/openapi-typed-request-handler", | ||
| "api:check": "api-extractor run --verbose --config ./api-extractor.json", | ||
| "api": "api-extractor run --local --verbose --config ./api-extractor.json" | ||
| }, | ||
| "repository": "github:MapColonies/infra-packages", | ||
| "files": [ | ||
| "dist/**/*" | ||
| ], | ||
| "publishConfig": { | ||
| "access": "public" | ||
| }, | ||
| "engines": { | ||
| "node": ">=24" | ||
| }, | ||
| "dependencies": { | ||
| "ts-essentials": "^10.1.1" | ||
| }, | ||
| "peerDependencies": { | ||
| "@types/express": "^4.17.21", | ||
| "openapi-typescript": "^7.4.1" | ||
| }, | ||
| "devDependencies": { | ||
| "@map-colonies/eslint-config": "workspace:^", | ||
| "@map-colonies/tsconfig": "workspace:^", | ||
| "@types/node": "catalog:", | ||
| "express": "5.2.1", | ||
| "eslint": "catalog:", | ||
| "rimraf": "catalog:", | ||
| "typescript": "catalog:", | ||
| "vitest-config": "workspace:^", | ||
| "vitest": "catalog:", | ||
| "@microsoft/api-extractor": "catalog:" | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
packages/openapi-typed-request-handler/src/common/types.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
| type HasContent<T> = [T] extends [{ content: any }] ? T['content']['application/json'] : never; | ||
|
|
||
| export type Methods = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'head' | 'options' | 'trace'; | ||
|
|
||
| export type OperationsTemplate = Record<string, any>; | ||
|
|
||
| export type PathsTemplate = Record< | ||
| string, | ||
| { | ||
| parameters: { | ||
| query?: any; | ||
| header?: any; | ||
| path?: any; | ||
| cookie?: any; | ||
| }; | ||
| } & { | ||
| [key in Methods]?: OperationsTemplate; | ||
| } | ||
| >; | ||
|
|
||
| export type ResponseObjectToFlat<T> = [T] extends [{ responses: any }] | ||
| ? { | ||
| [res in keyof T['responses']]: { status: res; body: HasContent<T['responses'][res]> }; | ||
| }[keyof T['responses']] | ||
| : never; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But in other packages it's like I did it
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the real name of the package.so let fix it here as suggested.