Skip to content

Commit ca511a9

Browse files
committed
feat: introduce a new common runtime
1 parent c5f7ef7 commit ca511a9

File tree

37 files changed

+393
-588
lines changed

37 files changed

+393
-588
lines changed

packages/typescript-axios-runtime/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"test": "jest"
3030
},
3131
"dependencies": {
32+
"@nahkies/typescript-common-runtime": "workspace:^",
3233
"qs": "^6.14.0",
3334
"tslib": "^2.8.1"
3435
},

packages/typescript-axios-runtime/src/main.ts

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
import {
2+
type Encoding,
3+
requestBodyToUrlSearchParams,
4+
} from "@nahkies/typescript-common-runtime/request-bodies/url-search-params"
5+
import type {
6+
HeaderParams,
7+
QueryParams,
8+
} from "@nahkies/typescript-common-runtime/types"
19
import axios, {
210
AxiosHeaders,
311
type AxiosInstance,
@@ -6,30 +14,12 @@ import axios, {
614
type RawAxiosRequestHeaders,
715
} from "axios"
816
import qs from "qs"
9-
import {
10-
type Encoding,
11-
requestBodyToUrlSearchParams,
12-
} from "./request-bodies/url-search-params"
13-
14-
export type QueryParams = {
15-
[name: string]:
16-
| string
17-
| number
18-
| number[]
19-
| boolean
20-
| string[]
21-
| undefined
22-
| null
23-
| QueryParams
24-
| QueryParams[]
25-
}
26-
27-
export type HeaderParams =
28-
| Record<string, string | number | boolean | undefined | null>
29-
| [string, string | number | boolean | undefined | null][]
30-
| Headers
3117

32-
export type Server<T> = string & {__server__: T}
18+
export type {
19+
HeaderParams,
20+
QueryParams,
21+
Server,
22+
} from "@nahkies/typescript-common-runtime/types"
3323

3424
export interface AbstractAxiosConfig {
3525
axios?: AxiosInstance

packages/typescript-axios-runtime/tsconfig.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@
55
"rootDir": "./src"
66
},
77
"include": ["src/**/*"],
8-
"references": []
8+
"references": [
9+
{
10+
"path": "../typescript-common-runtime"
11+
}
12+
]
913
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# @nahkies/typescript-common-runtime
2+
3+
[![CI/CD](https://github.com/mnahkies/openapi-code-generator/actions/workflows/ci.yml/badge.svg)](https://github.com/mnahkies/openapi-code-generator/actions?query=branch%3Amain+event%3Apush)
4+
[![npm](https://img.shields.io/npm/dm/%40nahkies%2Ftypescript-common-runtime.svg)](https://www.npmjs.com/package/@nahkies/typescript-common-runtime)
5+
6+
This is a supporting package for code generated using [@nahkies/openapi-code-generator](https://www.npmjs.com/package/@nahkies/openapi-code-generator) shared across the typescript templates.
7+
8+
You can [read the docs](https://openapi-code-generator.nahkies.co.nz/) to find out more!
9+
10+
It's not intended by be used standalone. Similar in spirit to [tslib](https://www.npmjs.com/package/tslib)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const base = require("../../jest.base")
2+
const {name: displayName} = require("./package.json")
3+
4+
/**
5+
* @type { import('@jest/types').Config.ProjectConfig }
6+
*/
7+
const config = {
8+
...base,
9+
displayName,
10+
}
11+
12+
module.exports = config
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
{
2+
"name": "@nahkies/typescript-common-runtime",
3+
"version": "0.22.0",
4+
"description": "Runtime package for code generated by @nahkies/openapi-code-generator using the typescript templates",
5+
"license": "MIT",
6+
"author": {
7+
"name": "Michael Nahkies",
8+
"email": "[email protected]"
9+
},
10+
"homepage": "https://openapi-code-generator.nahkies.co.nz/",
11+
"repository": {
12+
"type": "git",
13+
"url": "https://github.com/mnahkies/openapi-code-generator.git",
14+
"directory": "packages/typescript-common-runtime"
15+
},
16+
"bugs": {
17+
"url": "https://github.com/mnahkies/openapi-code-generator/issues"
18+
},
19+
"exports": {
20+
"./errors": {
21+
"require": "./dist/errors.js",
22+
"import": "./dist/errors.js",
23+
"types": "./dist/errors.d.ts"
24+
},
25+
"./validation": {
26+
"require": "./dist/validation.js",
27+
"import": "./dist/validation.js",
28+
"types": "./dist/validation.d.ts"
29+
},
30+
"./types": {
31+
"require": "./dist/types.js",
32+
"import": "./dist/types.js",
33+
"types": "./dist/types.d.ts"
34+
},
35+
"./request-bodies/url-search-params": {
36+
"require": "./dist/request-bodies/url-search-params.js",
37+
"import": "./dist/request-bodies/url-search-params.js",
38+
"types": "./dist/request-bodies/url-search-params.d.ts"
39+
}
40+
},
41+
"scripts": {
42+
"clean": "rm -rf ./dist && rm tsconfig.tsbuildinfo",
43+
"build": "tsc -p ./tsconfig.json",
44+
"test": "jest"
45+
},
46+
"dependencies": {
47+
"tslib": "^2.8.1"
48+
},
49+
"peerDependencies": {
50+
"joi": "^17.13.3 || ^18.0.1",
51+
"zod": "^3.25.74 || ^4.1.12"
52+
},
53+
"peerDependenciesMeta": {
54+
"joi": {
55+
"optional": true
56+
},
57+
"zod": {
58+
"optional": true
59+
}
60+
},
61+
"devDependencies": {
62+
"@jest/globals": "^30.2.0",
63+
"jest": "^30.2.0",
64+
"joi": "^18.0.2",
65+
"typescript": "^5.9.3",
66+
"zod": "^3.25.74"
67+
},
68+
"files": [
69+
"src",
70+
"dist",
71+
"README.md",
72+
"CHANGELOG.md",
73+
"tsconfig.json"
74+
],
75+
"keywords": [
76+
"@nahkies/openapi-code-generator",
77+
"openapi",
78+
"runtime",
79+
"typescript"
80+
],
81+
"publishConfig": {
82+
"access": "public"
83+
}
84+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export enum RequestInputType {
2+
RouteParam = "route params",
3+
QueryString = "querystring",
4+
RequestBody = "request body",
5+
RequestHeader = "request header",
6+
}
7+
8+
export abstract class AbstractRuntimeError extends Error {
9+
protected constructor(
10+
message: string,
11+
cause: unknown,
12+
public readonly phase:
13+
| "request_validation"
14+
| "request_handler"
15+
| "response_validation",
16+
) {
17+
super(message, {cause})
18+
}
19+
}

packages/typescript-axios-runtime/src/request-bodies/url-search-param.spec.ts renamed to packages/typescript-common-runtime/src/request-bodies/url-search-params.spec.ts

File renamed without changes.

packages/typescript-axios-runtime/src/request-bodies/url-search-params.ts renamed to packages/typescript-common-runtime/src/request-bodies/url-search-params.ts

File renamed without changes.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// from https://stackoverflow.com/questions/39494689/is-it-possible-to-restrict-number-to-a-certain-range
2+
type Enumerate<
3+
N extends number,
4+
Acc extends number[] = [],
5+
> = Acc["length"] extends N
6+
? Acc[number]
7+
: Enumerate<N, [...Acc, Acc["length"]]>
8+
9+
type IntRange<F extends number, T extends number> = F extends T
10+
? F
11+
: Exclude<Enumerate<T>, Enumerate<F>> extends never
12+
? never
13+
: Exclude<Enumerate<T>, Enumerate<F>> | T
14+
15+
export type StatusCode1xx = IntRange<100, 199>
16+
export type StatusCode2xx = IntRange<200, 299>
17+
export type StatusCode3xx = IntRange<300, 399>
18+
export type StatusCode4xx = IntRange<400, 499>
19+
export type StatusCode5xx = IntRange<500, 599>
20+
export type StatusCode =
21+
| StatusCode1xx
22+
| StatusCode2xx
23+
| StatusCode3xx
24+
| StatusCode4xx
25+
| StatusCode5xx
26+
27+
export type Response<Status extends StatusCode, Type> = {
28+
status: Status
29+
body: Type
30+
}
31+
32+
export type QueryParams = {
33+
[name: string]:
34+
| string
35+
| number
36+
| number[]
37+
| boolean
38+
| string[]
39+
| undefined
40+
| null
41+
| QueryParams
42+
| QueryParams[]
43+
}
44+
45+
export type HeaderParams =
46+
| Record<string, string | number | boolean | undefined | null>
47+
| [string, string | number | boolean | undefined | null][]
48+
| Headers
49+
50+
export type Server<T> = string & {__server__: T}

0 commit comments

Comments
 (0)