yedra 0.12.13 → 0.13.0
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/dist/routing/app.d.ts +5 -1
- package/dist/routing/app.js +23 -21
- package/dist/routing/rest.d.ts +2 -2
- package/dist/routing/rest.js +5 -6
- package/dist/routing/websocket.d.ts +1 -1
- package/package.json +3 -3
package/dist/routing/app.d.ts
CHANGED
|
@@ -17,7 +17,11 @@ export declare class Yedra {
|
|
|
17
17
|
* @param req - The HTTP request.
|
|
18
18
|
* @returns The HTTP response.
|
|
19
19
|
*/
|
|
20
|
-
|
|
20
|
+
fetch(url: URL | string, options?: {
|
|
21
|
+
method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
22
|
+
body?: string | Buffer;
|
|
23
|
+
headers?: Record<string, string>;
|
|
24
|
+
}): Promise<Response>;
|
|
21
25
|
/**
|
|
22
26
|
* Generate OpenAPI documentation for the app.
|
|
23
27
|
*/
|
package/dist/routing/app.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
+
import { readFile, readdir, stat } from 'node:fs/promises';
|
|
2
|
+
import { createServer } from 'node:http';
|
|
3
|
+
import { extname, join } from 'node:path';
|
|
4
|
+
import mime from 'mime';
|
|
1
5
|
import { WebSocketServer } from 'ws';
|
|
2
6
|
import { HttpError } from './errors.js';
|
|
3
7
|
import { Path } from './path.js';
|
|
4
|
-
import { createServer } from 'node:http';
|
|
5
8
|
import { RestEndpoint } from './rest.js';
|
|
6
9
|
import { WsEndpoint } from './websocket.js';
|
|
7
|
-
import { extname, join } from 'node:path';
|
|
8
|
-
import { readdir, readFile, stat } from 'node:fs/promises';
|
|
9
|
-
import mime from 'mime';
|
|
10
10
|
class Context {
|
|
11
11
|
constructor(server) {
|
|
12
12
|
this.server = server;
|
|
@@ -71,19 +71,21 @@ export class Yedra {
|
|
|
71
71
|
* @param req - The HTTP request.
|
|
72
72
|
* @returns The HTTP response.
|
|
73
73
|
*/
|
|
74
|
-
async
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
74
|
+
async fetch(url, options) {
|
|
75
|
+
const parsedUrl = typeof url === 'string' ? new URL(url, 'http://localhost') : url;
|
|
76
|
+
const method = options?.method ?? 'GET';
|
|
77
|
+
if (method !== 'GET' &&
|
|
78
|
+
method !== 'POST' &&
|
|
79
|
+
method !== 'PUT' &&
|
|
80
|
+
method !== 'DELETE') {
|
|
81
|
+
return Yedra.errorResponse(405, `Method '${method}' not allowed.`);
|
|
81
82
|
}
|
|
82
|
-
const match = this.matchRestRoute(
|
|
83
|
+
const match = this.matchRestRoute(parsedUrl.pathname, method);
|
|
83
84
|
if (!match.result) {
|
|
84
|
-
if (
|
|
85
|
+
if (method === 'GET') {
|
|
85
86
|
// try returning a static file
|
|
86
|
-
const staticFile = this.staticFiles.get(
|
|
87
|
+
const staticFile = this.staticFiles.get(parsedUrl.pathname) ??
|
|
88
|
+
this.staticFiles.get('__fallback');
|
|
87
89
|
if (staticFile !== undefined) {
|
|
88
90
|
return new Response(staticFile.data, {
|
|
89
91
|
headers: {
|
|
@@ -93,12 +95,12 @@ export class Yedra {
|
|
|
93
95
|
}
|
|
94
96
|
}
|
|
95
97
|
if (match.invalidMethod) {
|
|
96
|
-
return Yedra.errorResponse(405, `Method '${
|
|
98
|
+
return Yedra.errorResponse(405, `Method '${method}' not allowed for path '${parsedUrl.pathname}'.`);
|
|
97
99
|
}
|
|
98
|
-
return Yedra.errorResponse(404, `Path '${
|
|
100
|
+
return Yedra.errorResponse(404, `Path '${parsedUrl.pathname}' not found.`);
|
|
99
101
|
}
|
|
100
102
|
try {
|
|
101
|
-
return await match.result.endpoint.handle(
|
|
103
|
+
return await match.result.endpoint.handle(parsedUrl.pathname, options?.body ?? '', match.result.params, Object.fromEntries(parsedUrl.searchParams), options?.headers ?? {});
|
|
102
104
|
}
|
|
103
105
|
catch (error) {
|
|
104
106
|
if (error instanceof HttpError) {
|
|
@@ -138,14 +140,14 @@ export class Yedra {
|
|
|
138
140
|
});
|
|
139
141
|
req.on('end', async () => {
|
|
140
142
|
const body = chunks.length > 0 ? Buffer.concat(chunks) : undefined;
|
|
141
|
-
const response = await this.
|
|
143
|
+
const response = await this.fetch(url, {
|
|
142
144
|
method: req.method,
|
|
143
145
|
body: req.method === 'POST' || req.method === 'PUT' ? body : undefined,
|
|
144
|
-
headers: Object.entries(req.headers).map(([key, value]) => [
|
|
146
|
+
headers: Object.fromEntries(Object.entries(req.headers).map(([key, value]) => [
|
|
145
147
|
key,
|
|
146
148
|
Array.isArray(value) ? value.join(',') : (value ?? ''),
|
|
147
|
-
]),
|
|
148
|
-
})
|
|
149
|
+
])),
|
|
150
|
+
});
|
|
149
151
|
res.writeHead(response.status, Object.fromEntries(response.headers));
|
|
150
152
|
res.end(Buffer.from(await response.arrayBuffer()));
|
|
151
153
|
const duration = Date.now() - begin;
|
package/dist/routing/rest.d.ts
CHANGED
|
@@ -31,7 +31,7 @@ type EndpointOptions<Params extends Record<string, Schema<unknown>>, Query exten
|
|
|
31
31
|
};
|
|
32
32
|
export declare abstract class RestEndpoint {
|
|
33
33
|
abstract get method(): 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
34
|
-
abstract handle(
|
|
34
|
+
abstract handle(pathname: string, body: string | Buffer, params: Record<string, string>, query: Record<string, string>, headers: Record<string, string>): Promise<Response>;
|
|
35
35
|
abstract documentation(): object;
|
|
36
36
|
}
|
|
37
37
|
declare class ConcreteRestEndpoint<Params extends Record<string, Schema<unknown>>, Query extends Record<string, Schema<unknown>>, Headers extends Record<string, Schema<unknown>>, Req extends BodyType<unknown>, Res extends BodyType<unknown>> extends RestEndpoint {
|
|
@@ -42,7 +42,7 @@ declare class ConcreteRestEndpoint<Params extends Record<string, Schema<unknown>
|
|
|
42
42
|
private headersSchema;
|
|
43
43
|
constructor(method: 'GET' | 'POST' | 'PUT' | 'DELETE', options: EndpointOptions<Params, Query, Headers, Req, Res>);
|
|
44
44
|
get method(): 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
45
|
-
handle(
|
|
45
|
+
handle(url: string, body: string | Buffer, params: Record<string, string>, query: Record<string, string>, headers: Record<string, string>): Promise<Response>;
|
|
46
46
|
documentation(): object;
|
|
47
47
|
}
|
|
48
48
|
export declare class Get<Params extends Record<string, Schema<unknown>>, Query extends Record<string, Schema<unknown>>, Headers extends Record<string, Schema<unknown>>, Res extends BodyType<unknown>> extends ConcreteRestEndpoint<Params, Query, Headers, NoneBody, Res> {
|
package/dist/routing/rest.js
CHANGED
|
@@ -17,17 +17,16 @@ class ConcreteRestEndpoint extends RestEndpoint {
|
|
|
17
17
|
get method() {
|
|
18
18
|
return this._method;
|
|
19
19
|
}
|
|
20
|
-
async handle(
|
|
20
|
+
async handle(url, body, params, query, headers) {
|
|
21
21
|
let parsedBody;
|
|
22
22
|
let parsedParams;
|
|
23
23
|
let parsedQuery;
|
|
24
24
|
let parsedHeaders;
|
|
25
|
-
const url = new URL(req.url);
|
|
26
25
|
try {
|
|
27
|
-
parsedBody = this.options.req.deserialize(Buffer.from(
|
|
26
|
+
parsedBody = this.options.req.deserialize(typeof body === 'string' ? Buffer.from(body) : body, headers['content-type'] ?? 'application/octet-stream');
|
|
28
27
|
parsedParams = this.paramsSchema.parse(params);
|
|
29
|
-
parsedQuery = this.querySchema.parse(
|
|
30
|
-
parsedHeaders = this.headersSchema.parse(
|
|
28
|
+
parsedQuery = this.querySchema.parse(query);
|
|
29
|
+
parsedHeaders = this.headersSchema.parse(headers);
|
|
31
30
|
}
|
|
32
31
|
catch (error) {
|
|
33
32
|
if (error instanceof SyntaxError) {
|
|
@@ -39,7 +38,7 @@ class ConcreteRestEndpoint extends RestEndpoint {
|
|
|
39
38
|
throw error;
|
|
40
39
|
}
|
|
41
40
|
const response = await this.options.do({
|
|
42
|
-
url
|
|
41
|
+
url,
|
|
43
42
|
params: parsedParams,
|
|
44
43
|
query: parsedQuery,
|
|
45
44
|
headers: parsedHeaders,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import type { WebSocket as NodeWebSocket } from 'ws';
|
|
1
2
|
import type { Typeof } from '../validation/body.js';
|
|
2
3
|
import { type ObjectSchema } from '../validation/object.js';
|
|
3
4
|
import type { Schema } from '../validation/schema.js';
|
|
4
|
-
import type { WebSocket as NodeWebSocket } from 'ws';
|
|
5
5
|
type MessageCb = (message: Buffer) => Promise<void> | void;
|
|
6
6
|
type CloseCb = (code: number | undefined, reason: string | undefined) => Promise<void> | void;
|
|
7
7
|
declare class YedraWebSocket {
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "yedra",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"repository": "github:0codekit/yedra",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"devDependencies": {
|
|
7
|
-
"@biomejs/biome": "^1.9.
|
|
8
|
-
"@types/node": "^22.7.
|
|
7
|
+
"@biomejs/biome": "^1.9.4",
|
|
8
|
+
"@types/node": "^22.7.9",
|
|
9
9
|
"@types/uuid": "^10.0.0",
|
|
10
10
|
"typescript": "^5.6.3"
|
|
11
11
|
},
|