@hyperspan/framework 1.0.10 → 1.0.11
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/package.json +1 -1
- package/src/actions.ts +4 -4
- package/src/server.ts +30 -19
- package/src/types.ts +11 -6
package/package.json
CHANGED
package/src/actions.ts
CHANGED
|
@@ -109,12 +109,12 @@ export function createAction<T extends z.ZodObject<any, any>>(params: { name: st
|
|
|
109
109
|
_errorHandler = handler;
|
|
110
110
|
return api;
|
|
111
111
|
},
|
|
112
|
-
use(middleware: HS.MiddlewareFunction) {
|
|
113
|
-
route.use(middleware);
|
|
112
|
+
use(middleware: HS.MiddlewareFunction, opts?: HS.MiddlewareMethodOptions) {
|
|
113
|
+
route.use(middleware, opts);
|
|
114
114
|
return api;
|
|
115
115
|
},
|
|
116
|
-
middleware(middleware: Array<HS.MiddlewareFunction
|
|
117
|
-
route.middleware(middleware);
|
|
116
|
+
middleware(middleware: Array<HS.MiddlewareFunction>, opts?: HS.MiddlewareMethodOptions) {
|
|
117
|
+
route.middleware(middleware, opts);
|
|
118
118
|
return api;
|
|
119
119
|
},
|
|
120
120
|
fetch: route.fetch,
|
package/src/server.ts
CHANGED
|
@@ -122,11 +122,11 @@ export function createContext(req: Request, route?: HS.Route): HS.Context {
|
|
|
122
122
|
export function createRoute(config: Partial<HS.RouteConfig> = {}): HS.Route {
|
|
123
123
|
const _handlers: Record<string, HS.RouteHandler> = {};
|
|
124
124
|
let _errorHandler: HS.ErrorHandler | undefined = undefined;
|
|
125
|
-
let _middleware: Record<string, Array<HS.MiddlewareFunction>> = { '*': [] };
|
|
126
125
|
|
|
127
126
|
const api: HS.Route = {
|
|
128
127
|
_kind: 'hsRoute',
|
|
129
128
|
_config: config,
|
|
129
|
+
_middleware: { GET: [], POST: [], PUT: [], PATCH: [], DELETE: [], HEAD: [], OPTIONS: [], '*': [] },
|
|
130
130
|
_methods: () => Object.keys(_handlers),
|
|
131
131
|
_path() {
|
|
132
132
|
if (this._config.path) {
|
|
@@ -141,7 +141,7 @@ export function createRoute(config: Partial<HS.RouteConfig> = {}): HS.Route {
|
|
|
141
141
|
*/
|
|
142
142
|
get(handler: HS.RouteHandler, handlerOptions?: HS.RouteHandlerOptions) {
|
|
143
143
|
_handlers['GET'] = handler;
|
|
144
|
-
_middleware['GET'] = handlerOptions?.middleware || [];
|
|
144
|
+
api._middleware['GET'] = handlerOptions?.middleware || [];
|
|
145
145
|
return api;
|
|
146
146
|
},
|
|
147
147
|
/**
|
|
@@ -149,7 +149,7 @@ export function createRoute(config: Partial<HS.RouteConfig> = {}): HS.Route {
|
|
|
149
149
|
*/
|
|
150
150
|
post(handler: HS.RouteHandler, handlerOptions?: HS.RouteHandlerOptions) {
|
|
151
151
|
_handlers['POST'] = handler;
|
|
152
|
-
_middleware['POST'] = handlerOptions?.middleware || [];
|
|
152
|
+
api._middleware['POST'] = handlerOptions?.middleware || [];
|
|
153
153
|
return api;
|
|
154
154
|
},
|
|
155
155
|
/**
|
|
@@ -157,7 +157,7 @@ export function createRoute(config: Partial<HS.RouteConfig> = {}): HS.Route {
|
|
|
157
157
|
*/
|
|
158
158
|
put(handler: HS.RouteHandler, handlerOptions?: HS.RouteHandlerOptions) {
|
|
159
159
|
_handlers['PUT'] = handler;
|
|
160
|
-
_middleware['PUT'] = handlerOptions?.middleware || [];
|
|
160
|
+
api._middleware['PUT'] = handlerOptions?.middleware || [];
|
|
161
161
|
return api;
|
|
162
162
|
},
|
|
163
163
|
/**
|
|
@@ -165,7 +165,7 @@ export function createRoute(config: Partial<HS.RouteConfig> = {}): HS.Route {
|
|
|
165
165
|
*/
|
|
166
166
|
patch(handler: HS.RouteHandler, handlerOptions?: HS.RouteHandlerOptions) {
|
|
167
167
|
_handlers['PATCH'] = handler;
|
|
168
|
-
_middleware['PATCH'] = handlerOptions?.middleware || [];
|
|
168
|
+
api._middleware['PATCH'] = handlerOptions?.middleware || [];
|
|
169
169
|
return api;
|
|
170
170
|
},
|
|
171
171
|
/**
|
|
@@ -173,7 +173,7 @@ export function createRoute(config: Partial<HS.RouteConfig> = {}): HS.Route {
|
|
|
173
173
|
*/
|
|
174
174
|
delete(handler: HS.RouteHandler, handlerOptions?: HS.RouteHandlerOptions) {
|
|
175
175
|
_handlers['DELETE'] = handler;
|
|
176
|
-
_middleware['DELETE'] = handlerOptions?.middleware || [];
|
|
176
|
+
api._middleware['DELETE'] = handlerOptions?.middleware || [];
|
|
177
177
|
return api;
|
|
178
178
|
},
|
|
179
179
|
/**
|
|
@@ -181,7 +181,7 @@ export function createRoute(config: Partial<HS.RouteConfig> = {}): HS.Route {
|
|
|
181
181
|
*/
|
|
182
182
|
options(handler: HS.RouteHandler, handlerOptions?: HS.RouteHandlerOptions) {
|
|
183
183
|
_handlers['OPTIONS'] = handler;
|
|
184
|
-
_middleware['OPTIONS'] = handlerOptions?.middleware || [];
|
|
184
|
+
api._middleware['OPTIONS'] = handlerOptions?.middleware || [];
|
|
185
185
|
return api;
|
|
186
186
|
},
|
|
187
187
|
/**
|
|
@@ -189,7 +189,7 @@ export function createRoute(config: Partial<HS.RouteConfig> = {}): HS.Route {
|
|
|
189
189
|
*/
|
|
190
190
|
all(handler: HS.RouteHandler, handlerOptions?: HS.RouteHandlerOptions) {
|
|
191
191
|
_handlers['*'] = handler;
|
|
192
|
-
_middleware['*'] = handlerOptions?.middleware || [];
|
|
192
|
+
api._middleware['*'] = handlerOptions?.middleware || [];
|
|
193
193
|
return api;
|
|
194
194
|
},
|
|
195
195
|
/**
|
|
@@ -202,16 +202,24 @@ export function createRoute(config: Partial<HS.RouteConfig> = {}): HS.Route {
|
|
|
202
202
|
/**
|
|
203
203
|
* Add a middleware function to this route (for all HTTP methods) (non-destructive)
|
|
204
204
|
*/
|
|
205
|
-
use(middleware: HS.MiddlewareFunction) {
|
|
206
|
-
|
|
205
|
+
use(middleware: HS.MiddlewareFunction, opts: { method?: HS.MiddlewareMethod } = {}) {
|
|
206
|
+
if (opts.method) {
|
|
207
|
+
api._middleware[opts.method].push(middleware);
|
|
208
|
+
} else {
|
|
209
|
+
api._middleware['*'].push(middleware);
|
|
210
|
+
}
|
|
207
211
|
return api;
|
|
208
212
|
},
|
|
209
213
|
/**
|
|
210
214
|
* Set the complete middleware stack for this route (for all HTTP methods) (destructive)
|
|
211
215
|
* NOTE: This will override the middleware stack for this route
|
|
212
216
|
*/
|
|
213
|
-
middleware(middleware: Array<HS.MiddlewareFunction
|
|
214
|
-
|
|
217
|
+
middleware(middleware: Array<HS.MiddlewareFunction>, opts: { method?: HS.MiddlewareMethod } = {}) {
|
|
218
|
+
if (opts.method) {
|
|
219
|
+
api._middleware[opts.method] = middleware;
|
|
220
|
+
} else {
|
|
221
|
+
api._middleware['*'] = middleware;
|
|
222
|
+
}
|
|
215
223
|
return api;
|
|
216
224
|
},
|
|
217
225
|
|
|
@@ -220,9 +228,9 @@ export function createRoute(config: Partial<HS.RouteConfig> = {}): HS.Route {
|
|
|
220
228
|
*/
|
|
221
229
|
async fetch(request: Request) {
|
|
222
230
|
const context = createContext(request, api);
|
|
223
|
-
const method = context.req.method;
|
|
224
|
-
const globalMiddleware = _middleware['*'] || [];
|
|
225
|
-
const methodMiddleware = _middleware[method] || [];
|
|
231
|
+
const method = context.req.method as HS.MiddlewareMethod;
|
|
232
|
+
const globalMiddleware = api._middleware['*'] || [];
|
|
233
|
+
const methodMiddleware = api._middleware[method] || [];
|
|
226
234
|
|
|
227
235
|
const methodHandler = async (context: HS.Context) => {
|
|
228
236
|
// Handle CORS preflight requests (if no OPTIONS handler is defined)
|
|
@@ -306,7 +314,6 @@ export function createRoute(config: Partial<HS.RouteConfig> = {}): HS.Route {
|
|
|
306
314
|
* Creates a server object that can compose routes and middleware
|
|
307
315
|
*/
|
|
308
316
|
export async function createServer(config: HS.Config = {} as HS.Config): Promise<HS.Server> {
|
|
309
|
-
const _middleware: HS.MiddlewareFunction[] = [];
|
|
310
317
|
const _routes: HS.Route[] = [];
|
|
311
318
|
|
|
312
319
|
// Load plugins, if any
|
|
@@ -317,9 +324,13 @@ export async function createServer(config: HS.Config = {} as HS.Config): Promise
|
|
|
317
324
|
const api: HS.Server = {
|
|
318
325
|
_config: config,
|
|
319
326
|
_routes: _routes,
|
|
320
|
-
_middleware:
|
|
321
|
-
use(middleware: HS.MiddlewareFunction) {
|
|
322
|
-
|
|
327
|
+
_middleware: { GET: [], POST: [], PUT: [], PATCH: [], DELETE: [], HEAD: [], OPTIONS: [], '*': [] },
|
|
328
|
+
use(middleware: HS.MiddlewareFunction, opts?: HS.MiddlewareMethodOptions) {
|
|
329
|
+
if (opts?.method) {
|
|
330
|
+
api._middleware[opts.method].push(middleware);
|
|
331
|
+
} else {
|
|
332
|
+
api._middleware['*'].push(middleware);
|
|
333
|
+
}
|
|
323
334
|
return this;
|
|
324
335
|
},
|
|
325
336
|
get(path: string, handler: HS.RouteHandler, handlerOptions?: HS.RouteHandlerOptions) {
|
package/src/types.ts
CHANGED
|
@@ -8,8 +8,8 @@ export namespace Hyperspan {
|
|
|
8
8
|
export interface Server {
|
|
9
9
|
_config: Hyperspan.Config;
|
|
10
10
|
_routes: Array<Hyperspan.Route>;
|
|
11
|
-
_middleware: Array<Hyperspan.MiddlewareFunction
|
|
12
|
-
use: (middleware: Hyperspan.MiddlewareFunction) => Hyperspan.Server;
|
|
11
|
+
_middleware: Record<Hyperspan.MiddlewareMethod, Array<Hyperspan.MiddlewareFunction>>;
|
|
12
|
+
use: (middleware: Hyperspan.MiddlewareFunction, opts?: Hyperspan.MiddlewareMethodOptions) => Hyperspan.Server;
|
|
13
13
|
get: (path: string, handler: Hyperspan.RouteHandler, handlerOptions?: Hyperspan.RouteHandlerOptions) => Hyperspan.Route;
|
|
14
14
|
post: (path: string, handler: Hyperspan.RouteHandler, handlerOptions?: Hyperspan.RouteHandlerOptions) => Hyperspan.Route;
|
|
15
15
|
put: (path: string, handler: Hyperspan.RouteHandler, handlerOptions?: Hyperspan.RouteHandlerOptions) => Hyperspan.Route;
|
|
@@ -133,11 +133,16 @@ export namespace Hyperspan {
|
|
|
133
133
|
context: Hyperspan.Context,
|
|
134
134
|
next: Hyperspan.NextFunction
|
|
135
135
|
) => Promise<Response> | Response;
|
|
136
|
+
export type MiddlewareMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS' | '*';
|
|
137
|
+
export type MiddlewareMethodOptions = {
|
|
138
|
+
method?: Hyperspan.MiddlewareMethod;
|
|
139
|
+
};
|
|
136
140
|
|
|
137
141
|
export interface Route {
|
|
138
142
|
_kind: 'hsRoute';
|
|
139
143
|
_config: Partial<Hyperspan.RouteConfig>;
|
|
140
144
|
_serverConfig?: Hyperspan.Config;
|
|
145
|
+
_middleware: Record<MiddlewareMethod, Array<Hyperspan.MiddlewareFunction>>;
|
|
141
146
|
_path(): string;
|
|
142
147
|
_methods(): string[];
|
|
143
148
|
get: (handler: Hyperspan.RouteHandler, handlerOptions?: Hyperspan.RouteHandlerOptions) => Hyperspan.Route;
|
|
@@ -148,8 +153,8 @@ export namespace Hyperspan {
|
|
|
148
153
|
options: (handler: Hyperspan.RouteHandler, handlerOptions?: Hyperspan.RouteHandlerOptions) => Hyperspan.Route;
|
|
149
154
|
all: (handler: Hyperspan.RouteHandler, handlerOptions?: Hyperspan.RouteHandlerOptions) => Hyperspan.Route;
|
|
150
155
|
errorHandler: (handler: Hyperspan.ErrorHandler) => Hyperspan.Route;
|
|
151
|
-
use: (middleware: Hyperspan.MiddlewareFunction) => Hyperspan.Route;
|
|
152
|
-
middleware: (middleware: Array<Hyperspan.MiddlewareFunction
|
|
156
|
+
use: (middleware: Hyperspan.MiddlewareFunction, opts?: Hyperspan.MiddlewareMethodOptions) => Hyperspan.Route;
|
|
157
|
+
middleware: (middleware: Array<Hyperspan.MiddlewareFunction>, opts?: Hyperspan.MiddlewareMethodOptions) => Hyperspan.Route;
|
|
153
158
|
fetch: (request: Request) => Promise<Response>;
|
|
154
159
|
};
|
|
155
160
|
|
|
@@ -178,8 +183,8 @@ export namespace Hyperspan {
|
|
|
178
183
|
render: (c: Context, props?: ActionFormProps<T>) => ActionFormResponse;
|
|
179
184
|
post: (handler: ActionFormHandler<T>) => Action<T>;
|
|
180
185
|
errorHandler: (handler: ActionFormHandler<T>) => Action<T>;
|
|
181
|
-
use: (middleware: Hyperspan.MiddlewareFunction) => Action<T>;
|
|
182
|
-
middleware: (middleware: Array<Hyperspan.MiddlewareFunction
|
|
186
|
+
use: (middleware: Hyperspan.MiddlewareFunction, opts?: Hyperspan.MiddlewareMethodOptions) => Action<T>;
|
|
187
|
+
middleware: (middleware: Array<Hyperspan.MiddlewareFunction>, opts?: Hyperspan.MiddlewareMethodOptions) => Action<T>;
|
|
183
188
|
fetch: (request: Request) => Promise<Response>;
|
|
184
189
|
}
|
|
185
190
|
|