barejs 0.1.47 → 0.1.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -20
- package/README.md +221 -184
- package/dist/index.js +122 -187
- package/dist/types/client/index.d.ts +48 -0
- package/dist/types/core/bare.d.ts +14 -1
- package/dist/types/core/context.d.ts +9 -1
- package/dist/types/core/router.d.ts +1 -0
- package/dist/types/index.d.ts +6 -2
- package/dist/types/security/auth.d.ts +2 -2
- package/dist/types/security/headers.d.ts +9 -0
- package/dist/types/security/rateLimit.d.ts +9 -0
- package/dist/types/utils/openapi.d.ts +15 -0
- package/package.json +68 -68
- package/src/client/index.ts +121 -0
- package/src/core/bare.ts +463 -318
- package/src/core/config.ts +31 -31
- package/src/core/context.ts +205 -111
- package/src/core/loader.ts +12 -12
- package/src/core/radix.ts +184 -171
- package/src/core/router.ts +61 -62
- package/src/index.ts +73 -61
- package/src/security/auth.ts +174 -153
- package/src/security/cors.ts +45 -26
- package/src/security/hash.ts +22 -19
- package/src/security/headers.ts +59 -0
- package/src/security/rateLimit.ts +76 -0
- package/src/security/validators.ts +137 -133
- package/src/utils/openapi.ts +116 -0
- package/src/utils/static.ts +42 -25
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export interface ClientRequestOptions {
|
|
2
|
+
params?: Record<string, string | number>;
|
|
3
|
+
query?: Record<string, string | number | boolean | undefined>;
|
|
4
|
+
body?: any;
|
|
5
|
+
headers?: Record<string, string>;
|
|
6
|
+
}
|
|
7
|
+
export interface BareClientOptions {
|
|
8
|
+
headers?: Record<string, string>;
|
|
9
|
+
fetch?: typeof fetch;
|
|
10
|
+
}
|
|
11
|
+
type ExtractParams<Path extends string> = Path extends `${string}:${infer Param}/${infer Rest}` ? {
|
|
12
|
+
[K in Param | keyof ExtractParams<`/${Rest}`>]: string | number;
|
|
13
|
+
} : Path extends `${string}:${infer Param}` ? {
|
|
14
|
+
[K in Param]: string | number;
|
|
15
|
+
} : Record<string, never>;
|
|
16
|
+
export type RouteSchema = Record<string, {
|
|
17
|
+
method: string;
|
|
18
|
+
params?: Record<string, any>;
|
|
19
|
+
body?: any;
|
|
20
|
+
response?: any;
|
|
21
|
+
}>;
|
|
22
|
+
type PathsForMethod<Routes extends RouteSchema, M extends string> = {
|
|
23
|
+
[K in keyof Routes]: Routes[K]['method'] extends M ? K : never;
|
|
24
|
+
}[keyof Routes] & string;
|
|
25
|
+
export interface BareClient<Routes extends RouteSchema = RouteSchema> {
|
|
26
|
+
get<P extends PathsForMethod<Routes, 'GET'> | (string & {})>(path: P, options?: ClientRequestOptions & (P extends keyof Routes ? {
|
|
27
|
+
params?: ExtractParams<P & string>;
|
|
28
|
+
} : {})): Promise<P extends keyof Routes ? Routes[P]['response'] : any>;
|
|
29
|
+
post<P extends PathsForMethod<Routes, 'POST'> | (string & {})>(path: P, options?: ClientRequestOptions & (P extends keyof Routes ? {
|
|
30
|
+
params?: ExtractParams<P & string>;
|
|
31
|
+
body?: Routes[P]['body'];
|
|
32
|
+
} : {})): Promise<P extends keyof Routes ? Routes[P]['response'] : any>;
|
|
33
|
+
put<P extends PathsForMethod<Routes, 'PUT'> | (string & {})>(path: P, options?: ClientRequestOptions & (P extends keyof Routes ? {
|
|
34
|
+
params?: ExtractParams<P & string>;
|
|
35
|
+
body?: Routes[P]['body'];
|
|
36
|
+
} : {})): Promise<P extends keyof Routes ? Routes[P]['response'] : any>;
|
|
37
|
+
patch<P extends PathsForMethod<Routes, 'PATCH'> | (string & {})>(path: P, options?: ClientRequestOptions & (P extends keyof Routes ? {
|
|
38
|
+
params?: ExtractParams<P & string>;
|
|
39
|
+
body?: Routes[P]['body'];
|
|
40
|
+
} : {})): Promise<P extends keyof Routes ? Routes[P]['response'] : any>;
|
|
41
|
+
delete<P extends PathsForMethod<Routes, 'DELETE'> | (string & {})>(path: P, options?: ClientRequestOptions & (P extends keyof Routes ? {
|
|
42
|
+
params?: ExtractParams<P & string>;
|
|
43
|
+
} : {})): Promise<P extends keyof Routes ? Routes[P]['response'] : any>;
|
|
44
|
+
}
|
|
45
|
+
export declare const bareClient: <AppOrRoutes = any>(baseUrl: string, clientOptions?: BareClientOptions) => BareClient<AppOrRoutes extends {
|
|
46
|
+
__routes?: infer R extends RouteSchema;
|
|
47
|
+
} ? R : RouteSchema>;
|
|
48
|
+
export {};
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import { BareRouter } from './router';
|
|
2
2
|
import { Context } from './context';
|
|
3
3
|
import { Type as typebox, Type as t } from '@sinclair/typebox';
|
|
4
|
+
import { type OpenAPIOptions } from "../utils/openapi";
|
|
5
|
+
export interface WebSocketRouteHandler {
|
|
6
|
+
open?: (ws: any) => void;
|
|
7
|
+
message?: (ws: any, message: string | Buffer) => void;
|
|
8
|
+
close?: (ws: any, code: number, reason: string) => void;
|
|
9
|
+
drain?: (ws: any) => void;
|
|
10
|
+
}
|
|
4
11
|
export interface NotFoundResponse {
|
|
5
12
|
status: number;
|
|
6
13
|
message: string;
|
|
@@ -13,14 +20,20 @@ export declare class BareJS extends BareRouter {
|
|
|
13
20
|
private compiled;
|
|
14
21
|
private poolMask;
|
|
15
22
|
private _useLog;
|
|
23
|
+
private errorHandler?;
|
|
24
|
+
private wsRoutes;
|
|
16
25
|
useLog(enabled?: boolean): this;
|
|
17
26
|
constructor(poolSize?: number);
|
|
18
27
|
private notFoundHandler;
|
|
19
28
|
setNotFound(handler: () => NotFoundResponse): void;
|
|
29
|
+
onError(handler: (err: any, ctx: Context) => any): this;
|
|
30
|
+
ws(path: string, handler: WebSocketRouteHandler): this;
|
|
31
|
+
docs(path?: string, options?: OpenAPIOptions): this;
|
|
32
|
+
rpc(path: string, procedures: Record<string, (params: any, ctx: Context) => any>): this;
|
|
20
33
|
private compileHandler;
|
|
21
34
|
compile(): void;
|
|
22
35
|
fetch: (req: Request) => any;
|
|
23
36
|
name: string;
|
|
24
|
-
listen(port?: number, hostname?: string, reusePort?: boolean):
|
|
37
|
+
listen(port?: number, hostname?: string, reusePort?: boolean): Bun.Server<WebSocketRouteHandler> | Bun.Server<undefined>;
|
|
25
38
|
}
|
|
26
39
|
export { Context, typebox, t };
|
|
@@ -11,8 +11,10 @@ export declare class Context {
|
|
|
11
11
|
_headers?: Record<string, string> | undefined;
|
|
12
12
|
store: Map<string, any>;
|
|
13
13
|
body: any;
|
|
14
|
-
|
|
14
|
+
_query: Record<string, string> | undefined;
|
|
15
15
|
constructor();
|
|
16
|
+
get query(): Record<string, string>;
|
|
17
|
+
getQuery(key: string): string | undefined;
|
|
16
18
|
jsonBody(): Promise<any>;
|
|
17
19
|
reset(req: Request): this;
|
|
18
20
|
status(code: number): this;
|
|
@@ -21,4 +23,10 @@ export declare class Context {
|
|
|
21
23
|
get(key: string): any;
|
|
22
24
|
setHeader(name: string, value: string): this;
|
|
23
25
|
send(message: string, extra?: Record<string, any>): Response;
|
|
26
|
+
sse(source: AsyncIterable<string | {
|
|
27
|
+
event?: string;
|
|
28
|
+
data: any;
|
|
29
|
+
id?: string;
|
|
30
|
+
retry?: number;
|
|
31
|
+
}> | ((send: (data: any, event?: string, id?: string) => void, close: () => void) => Promise<void> | void)): Response;
|
|
24
32
|
}
|
|
@@ -11,6 +11,7 @@ export declare class BareRouter {
|
|
|
11
11
|
put: (path: string, ...h: HandlersChain) => this;
|
|
12
12
|
patch: (path: string, ...h: HandlersChain) => this;
|
|
13
13
|
delete: (path: string, ...h: HandlersChain) => this;
|
|
14
|
+
options: (path: string, ...h: HandlersChain) => this;
|
|
14
15
|
group: (path: string, ...args: any[]) => this;
|
|
15
16
|
use(mw: any): this;
|
|
16
17
|
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Built for Bun with Mechanical Sympathy
|
|
5
5
|
* Optimized to outperform competitors by 55% [2026-01-05]
|
|
6
6
|
*/
|
|
7
|
-
export { BareJS } from './core/bare';
|
|
7
|
+
export { BareJS, type WebSocketRouteHandler } from './core/bare';
|
|
8
8
|
/**
|
|
9
9
|
* Plugin Interface for BareJS
|
|
10
10
|
*/
|
|
@@ -17,7 +17,11 @@ export { Type, Type as T, Type as TypeBox };
|
|
|
17
17
|
import { typebox, zod, native, validate, Z } from './security/validators';
|
|
18
18
|
export { typebox, typebox as t, zod, zod as z, Z, Z as Zod, native, native as n, validate, validate as v };
|
|
19
19
|
export { cors } from './security/cors';
|
|
20
|
+
export { rateLimit, type RateLimitOptions } from './security/rateLimit';
|
|
21
|
+
export { securityHeaders, type SecurityHeadersOptions } from './security/headers';
|
|
20
22
|
export { staticFile } from './utils/static';
|
|
23
|
+
export { generateOpenAPISpec, renderDocsHTML, type OpenAPIOptions } from './utils/openapi';
|
|
21
24
|
export { BareRouter } from './core/router';
|
|
22
|
-
export { bareAuth, createToken } from './security/auth';
|
|
25
|
+
export { bareAuth, createToken, basicAuth, hasRole } from './security/auth';
|
|
23
26
|
export { Hash as Password, Hash } from './security/hash';
|
|
27
|
+
export { bareClient, type BareClient, type BareClientOptions, type ClientRequestOptions, type RouteSchema } from './client/index';
|
|
@@ -5,9 +5,9 @@ import type { Next } from '../core/context';
|
|
|
5
5
|
export declare const createToken: (payload: object, secret: string) => Promise<string>;
|
|
6
6
|
/**
|
|
7
7
|
* 1. BARE TOKEN AUTH (Standard JWT Middleware)
|
|
8
|
-
* High-performance JWT
|
|
8
|
+
* High-performance, constant-time JWT middleware for BareJS
|
|
9
9
|
*/
|
|
10
|
-
export declare const bareAuth: (secret: string) => (ctx: any, next: any) =>
|
|
10
|
+
export declare const bareAuth: (secret: string) => (ctx: any, next: any) => any;
|
|
11
11
|
/**
|
|
12
12
|
* 2. BASIC AUTH
|
|
13
13
|
* Useful for internal tools and simple admin panels
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Middleware } from '../core/context';
|
|
2
|
+
export interface SecurityHeadersOptions {
|
|
3
|
+
contentSecurityPolicy?: string | false;
|
|
4
|
+
frameOptions?: 'DENY' | 'SAMEORIGIN' | false;
|
|
5
|
+
hsts?: string | false;
|
|
6
|
+
referrerPolicy?: string | false;
|
|
7
|
+
noSniff?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare const securityHeaders: (options?: SecurityHeadersOptions) => Middleware;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Context, Middleware } from '../core/context';
|
|
2
|
+
export interface RateLimitOptions {
|
|
3
|
+
windowMs?: number;
|
|
4
|
+
max?: number;
|
|
5
|
+
message?: string;
|
|
6
|
+
keyGenerator?: (ctx: Context) => string;
|
|
7
|
+
maxEntries?: number;
|
|
8
|
+
}
|
|
9
|
+
export declare const rateLimit: (options?: RateLimitOptions) => Middleware;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface OpenAPIOptions {
|
|
2
|
+
title?: string;
|
|
3
|
+
version?: string;
|
|
4
|
+
description?: string;
|
|
5
|
+
}
|
|
6
|
+
export declare const generateOpenAPISpec: (routes: any[], options?: OpenAPIOptions) => {
|
|
7
|
+
openapi: string;
|
|
8
|
+
info: {
|
|
9
|
+
description?: string | undefined;
|
|
10
|
+
title: string;
|
|
11
|
+
version: string;
|
|
12
|
+
};
|
|
13
|
+
paths: Record<string, Record<string, any>>;
|
|
14
|
+
};
|
|
15
|
+
export declare const renderDocsHTML: (specUrl: string, title?: string) => string;
|
package/package.json
CHANGED
|
@@ -1,68 +1,68 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "barejs",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"author": "xarhang",
|
|
5
|
-
"description": "High-performance JIT-specialized web framework for Bun",
|
|
6
|
-
"main": "./dist/index.js",
|
|
7
|
-
"module": "./dist/index.js",
|
|
8
|
-
"types": "./dist/types/index.d.ts",
|
|
9
|
-
"type": "module",
|
|
10
|
-
"exports": {
|
|
11
|
-
".": {
|
|
12
|
-
"import": "./dist/index.js",
|
|
13
|
-
"types": "./dist/types/index.d.ts"
|
|
14
|
-
}
|
|
15
|
-
},
|
|
16
|
-
"sideEffects": false,
|
|
17
|
-
"repository": {
|
|
18
|
-
"type": "git",
|
|
19
|
-
"url": "git+https://github.com/xarhang/barejs.git"
|
|
20
|
-
},
|
|
21
|
-
"scripts": {
|
|
22
|
-
"test": "bun test",
|
|
23
|
-
"benchs": "bun run benchmarks/index.ts",
|
|
24
|
-
"bench": "bun run benchmarks/head-to-head.ts",
|
|
25
|
-
"dev": "bun --watch example/index.ts",
|
|
26
|
-
"build": "bun ./scripts/build.ts",
|
|
27
|
-
"prepublishOnly": "bun run build",
|
|
28
|
-
"release": "bun run build && npm version patch && npm publish --access public"
|
|
29
|
-
},
|
|
30
|
-
"keywords": [
|
|
31
|
-
"bun",
|
|
32
|
-
"framework",
|
|
33
|
-
"fastest",
|
|
34
|
-
"jit",
|
|
35
|
-
"typebox",
|
|
36
|
-
"zod",
|
|
37
|
-
"barejs",
|
|
38
|
-
"http-server"
|
|
39
|
-
],
|
|
40
|
-
"license": "MIT",
|
|
41
|
-
"peerDependencies": {
|
|
42
|
-
"@sinclair/typebox": "^0.34.46",
|
|
43
|
-
"zod": "^3.0.0"
|
|
44
|
-
},
|
|
45
|
-
"peerDependenciesMeta": {
|
|
46
|
-
"@sinclair/typebox": {
|
|
47
|
-
"optional": true
|
|
48
|
-
},
|
|
49
|
-
"zod": {
|
|
50
|
-
"optional": true
|
|
51
|
-
}
|
|
52
|
-
},
|
|
53
|
-
"devDependencies": {
|
|
54
|
-
"@sinclair/typebox": "^0.34.47",
|
|
55
|
-
"@types/bun": "^1.3.5",
|
|
56
|
-
"elysia": "latest",
|
|
57
|
-
"hono": "latest",
|
|
58
|
-
"mitata": "^1.0.34",
|
|
59
|
-
"typescript": "latest",
|
|
60
|
-
"zod": "^3.25.76"
|
|
61
|
-
},
|
|
62
|
-
"files": [
|
|
63
|
-
"dist",
|
|
64
|
-
"src",
|
|
65
|
-
"README.md",
|
|
66
|
-
"LICENSE"
|
|
67
|
-
]
|
|
68
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "barejs",
|
|
3
|
+
"version": "0.1.49",
|
|
4
|
+
"author": "xarhang",
|
|
5
|
+
"description": "High-performance JIT-specialized web framework for Bun",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/types/index.d.ts",
|
|
9
|
+
"type": "module",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"types": "./dist/types/index.d.ts"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/xarhang/barejs.git"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"test": "bun test",
|
|
23
|
+
"benchs": "bun run benchmarks/index.ts",
|
|
24
|
+
"bench": "bun run benchmarks/head-to-head.ts",
|
|
25
|
+
"dev": "bun --watch example/index.ts",
|
|
26
|
+
"build": "bun ./scripts/build.ts",
|
|
27
|
+
"prepublishOnly": "bun run build",
|
|
28
|
+
"release": "bun run build && npm version patch && npm publish --access public"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"bun",
|
|
32
|
+
"framework",
|
|
33
|
+
"fastest",
|
|
34
|
+
"jit",
|
|
35
|
+
"typebox",
|
|
36
|
+
"zod",
|
|
37
|
+
"barejs",
|
|
38
|
+
"http-server"
|
|
39
|
+
],
|
|
40
|
+
"license": "MIT",
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"@sinclair/typebox": "^0.34.46",
|
|
43
|
+
"zod": "^3.0.0"
|
|
44
|
+
},
|
|
45
|
+
"peerDependenciesMeta": {
|
|
46
|
+
"@sinclair/typebox": {
|
|
47
|
+
"optional": true
|
|
48
|
+
},
|
|
49
|
+
"zod": {
|
|
50
|
+
"optional": true
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@sinclair/typebox": "^0.34.47",
|
|
55
|
+
"@types/bun": "^1.3.5",
|
|
56
|
+
"elysia": "latest",
|
|
57
|
+
"hono": "latest",
|
|
58
|
+
"mitata": "^1.0.34",
|
|
59
|
+
"typescript": "latest",
|
|
60
|
+
"zod": "^3.25.76"
|
|
61
|
+
},
|
|
62
|
+
"files": [
|
|
63
|
+
"dist",
|
|
64
|
+
"src",
|
|
65
|
+
"README.md",
|
|
66
|
+
"LICENSE"
|
|
67
|
+
]
|
|
68
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
export interface ClientRequestOptions {
|
|
2
|
+
params?: Record<string, string | number>;
|
|
3
|
+
query?: Record<string, string | number | boolean | undefined>;
|
|
4
|
+
body?: any;
|
|
5
|
+
headers?: Record<string, string>;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface BareClientOptions {
|
|
9
|
+
headers?: Record<string, string>;
|
|
10
|
+
fetch?: typeof fetch;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
type ExtractParams<Path extends string> =
|
|
14
|
+
Path extends `${string}:${infer Param}/${infer Rest}`
|
|
15
|
+
? { [K in Param | keyof ExtractParams<`/${Rest}`>]: string | number }
|
|
16
|
+
: Path extends `${string}:${infer Param}`
|
|
17
|
+
? { [K in Param]: string | number }
|
|
18
|
+
: Record<string, never>;
|
|
19
|
+
|
|
20
|
+
export type RouteSchema = Record<string, {
|
|
21
|
+
method: string;
|
|
22
|
+
params?: Record<string, any>;
|
|
23
|
+
body?: any;
|
|
24
|
+
response?: any;
|
|
25
|
+
}>;
|
|
26
|
+
|
|
27
|
+
type PathsForMethod<Routes extends RouteSchema, M extends string> = {
|
|
28
|
+
[K in keyof Routes]: Routes[K]['method'] extends M ? K : never;
|
|
29
|
+
}[keyof Routes] & string;
|
|
30
|
+
|
|
31
|
+
export interface BareClient<Routes extends RouteSchema = RouteSchema> {
|
|
32
|
+
get<P extends PathsForMethod<Routes, 'GET'> | (string & {})>(
|
|
33
|
+
path: P,
|
|
34
|
+
options?: ClientRequestOptions & (P extends keyof Routes ? { params?: ExtractParams<P & string> } : {})
|
|
35
|
+
): Promise<P extends keyof Routes ? Routes[P]['response'] : any>;
|
|
36
|
+
|
|
37
|
+
post<P extends PathsForMethod<Routes, 'POST'> | (string & {})>(
|
|
38
|
+
path: P,
|
|
39
|
+
options?: ClientRequestOptions & (P extends keyof Routes ? { params?: ExtractParams<P & string>; body?: Routes[P]['body'] } : {})
|
|
40
|
+
): Promise<P extends keyof Routes ? Routes[P]['response'] : any>;
|
|
41
|
+
|
|
42
|
+
put<P extends PathsForMethod<Routes, 'PUT'> | (string & {})>(
|
|
43
|
+
path: P,
|
|
44
|
+
options?: ClientRequestOptions & (P extends keyof Routes ? { params?: ExtractParams<P & string>; body?: Routes[P]['body'] } : {})
|
|
45
|
+
): Promise<P extends keyof Routes ? Routes[P]['response'] : any>;
|
|
46
|
+
|
|
47
|
+
patch<P extends PathsForMethod<Routes, 'PATCH'> | (string & {})>(
|
|
48
|
+
path: P,
|
|
49
|
+
options?: ClientRequestOptions & (P extends keyof Routes ? { params?: ExtractParams<P & string>; body?: Routes[P]['body'] } : {})
|
|
50
|
+
): Promise<P extends keyof Routes ? Routes[P]['response'] : any>;
|
|
51
|
+
|
|
52
|
+
delete<P extends PathsForMethod<Routes, 'DELETE'> | (string & {})>(
|
|
53
|
+
path: P,
|
|
54
|
+
options?: ClientRequestOptions & (P extends keyof Routes ? { params?: ExtractParams<P & string> } : {})
|
|
55
|
+
): Promise<P extends keyof Routes ? Routes[P]['response'] : any>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const buildUrl = (baseUrl: string, path: string, options?: ClientRequestOptions): string => {
|
|
59
|
+
let finalPath = path;
|
|
60
|
+
if (options?.params) {
|
|
61
|
+
for (const [k, v] of Object.entries(options.params)) {
|
|
62
|
+
finalPath = finalPath.replace(`:${k}`, encodeURIComponent(String(v)));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const base = baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl;
|
|
66
|
+
const normalizedPath = finalPath.startsWith('/') ? finalPath : `/${finalPath}`;
|
|
67
|
+
let url = `${base}${normalizedPath}`;
|
|
68
|
+
|
|
69
|
+
if (options?.query) {
|
|
70
|
+
const qs = new URLSearchParams();
|
|
71
|
+
for (const [k, v] of Object.entries(options.query)) {
|
|
72
|
+
if (v !== undefined) qs.set(k, String(v));
|
|
73
|
+
}
|
|
74
|
+
const str = qs.toString();
|
|
75
|
+
if (str) url += `?${str}`;
|
|
76
|
+
}
|
|
77
|
+
return url;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export const bareClient = <AppOrRoutes = any>(
|
|
81
|
+
baseUrl: string,
|
|
82
|
+
clientOptions: BareClientOptions = {}
|
|
83
|
+
): BareClient<AppOrRoutes extends { __routes?: infer R extends RouteSchema } ? R : RouteSchema> => {
|
|
84
|
+
const fetchImpl = clientOptions.fetch ?? fetch;
|
|
85
|
+
const defaultHeaders = clientOptions.headers ?? {};
|
|
86
|
+
|
|
87
|
+
const request = async (method: string, path: string, options?: ClientRequestOptions) => {
|
|
88
|
+
const url = buildUrl(baseUrl, path, options);
|
|
89
|
+
const headers: Record<string, string> = {
|
|
90
|
+
...defaultHeaders,
|
|
91
|
+
...(options?.headers ?? {})
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const init: RequestInit = { method, headers };
|
|
95
|
+
if (options?.body !== undefined && method !== 'GET' && method !== 'HEAD') {
|
|
96
|
+
if (!headers['Content-Type'] && !headers['content-type']) {
|
|
97
|
+
headers['Content-Type'] = 'application/json';
|
|
98
|
+
}
|
|
99
|
+
init.body = typeof options.body === 'string' ? options.body : JSON.stringify(options.body);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const res = await fetchImpl(url, init);
|
|
103
|
+
const ct = res.headers.get('content-type') || '';
|
|
104
|
+
const data = ct.includes('application/json') ? await res.json() : await res.text();
|
|
105
|
+
if (!res.ok) {
|
|
106
|
+
const err: any = new Error(`HTTP ${res.status}`);
|
|
107
|
+
err.status = res.status;
|
|
108
|
+
err.data = data;
|
|
109
|
+
throw err;
|
|
110
|
+
}
|
|
111
|
+
return data;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
get: (path: string, options?: ClientRequestOptions) => request('GET', path, options),
|
|
116
|
+
post: (path: string, options?: ClientRequestOptions) => request('POST', path, options),
|
|
117
|
+
put: (path: string, options?: ClientRequestOptions) => request('PUT', path, options),
|
|
118
|
+
patch: (path: string, options?: ClientRequestOptions) => request('PATCH', path, options),
|
|
119
|
+
delete: (path: string, options?: ClientRequestOptions) => request('DELETE', path, options)
|
|
120
|
+
} as any;
|
|
121
|
+
};
|