@whatwg-node/server 0.7.1 → 0.7.2-alpha-20230301144304-75f91ef
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/README.md +1 -61
- package/index.js +9 -3
- package/index.mjs +9 -3
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -291,12 +291,7 @@ const myServerAdapter = createServerAdapter(async request => {
|
|
|
291
291
|
// Select the other text parameter
|
|
292
292
|
const regularTextData = formData.get('additionalStuff')
|
|
293
293
|
// ...
|
|
294
|
-
return
|
|
295
|
-
status: 200,
|
|
296
|
-
headers: {
|
|
297
|
-
'Content-Type': 'application/json'
|
|
298
|
-
}
|
|
299
|
-
})
|
|
294
|
+
return Response.json({ message: 'ok' })
|
|
300
295
|
})
|
|
301
296
|
```
|
|
302
297
|
|
|
@@ -309,58 +304,3 @@ We'd recommend to use `@whatwg-node/router` to handle routing and middleware app
|
|
|
309
304
|
`@whatwg-node/server` under the hood.
|
|
310
305
|
|
|
311
306
|
> Learn more about `@whatwg-node/router` [here](../router)
|
|
312
|
-
|
|
313
|
-
### Basic Routing
|
|
314
|
-
|
|
315
|
-
```ts
|
|
316
|
-
// Then use it in any environment
|
|
317
|
-
import { createServer } from 'http'
|
|
318
|
-
import { createRouter, Router } from '@whatwg-node/router'
|
|
319
|
-
|
|
320
|
-
const router = createRouter()
|
|
321
|
-
// GET collection index
|
|
322
|
-
router.get('/todos', () => new Response('Todos Index!'))
|
|
323
|
-
// GET item
|
|
324
|
-
router.get('/todos/:id', ({ params }) => new Response(`Todo #${params.id}`))
|
|
325
|
-
// POST to the collection (we'll use async here)
|
|
326
|
-
router.post('/todos', async request => {
|
|
327
|
-
const content = await request.json()
|
|
328
|
-
return new Response('Creating Todo: ' + JSON.stringify(content))
|
|
329
|
-
})
|
|
330
|
-
|
|
331
|
-
// Redirect to a URL
|
|
332
|
-
router.get('/google', () => Response.redirect('http://www.google.com'))
|
|
333
|
-
|
|
334
|
-
// 404 for everything else
|
|
335
|
-
router.all('*', () => new Response('Not Found.', { status: 404 }))
|
|
336
|
-
|
|
337
|
-
const httpServer = createServer(router)
|
|
338
|
-
httpServer.listen(4000)
|
|
339
|
-
```
|
|
340
|
-
|
|
341
|
-
### Middlewares to handle CORS, cookies and more
|
|
342
|
-
|
|
343
|
-
This package also provides some utilities for your platform agnostic server implementation. The
|
|
344
|
-
following example shows how to get the cookies as an object from the request.
|
|
345
|
-
|
|
346
|
-
```ts
|
|
347
|
-
import { withCookies } from '@whatwg-node/server'
|
|
348
|
-
|
|
349
|
-
router.get('/foo', withCookies, ({ cookies }) => {
|
|
350
|
-
// cookies are parsed from the header into request.cookies
|
|
351
|
-
return new Response(`Cookies: ${JSON.stringify(cookies)}`)
|
|
352
|
-
})
|
|
353
|
-
```
|
|
354
|
-
|
|
355
|
-
You can also setup a CORS middleware to handle preflight CORS requests.
|
|
356
|
-
|
|
357
|
-
```ts
|
|
358
|
-
import { withCors } from '@whatwg-node/server'
|
|
359
|
-
|
|
360
|
-
const corsWithRouter = withCors(router, {
|
|
361
|
-
origin: 'http://localhost:4000',
|
|
362
|
-
methods: 'GET, POST, PATCH, DELETE',
|
|
363
|
-
headers: 'authorization, referer, origin, content-type',
|
|
364
|
-
credentials: false
|
|
365
|
-
})
|
|
366
|
-
```
|
package/index.js
CHANGED
|
@@ -417,7 +417,7 @@ function createServerAdapter(serverAdapterBaseObject, options) {
|
|
|
417
417
|
handleEvent,
|
|
418
418
|
handle: genericRequestHandler,
|
|
419
419
|
};
|
|
420
|
-
|
|
420
|
+
const serverAdapter = new Proxy(genericRequestHandler, {
|
|
421
421
|
// It should have all the attributes of the handler function and the server instance
|
|
422
422
|
has: (_, prop) => {
|
|
423
423
|
return (prop in adapterObj ||
|
|
@@ -443,7 +443,12 @@ function createServerAdapter(serverAdapterBaseObject, options) {
|
|
|
443
443
|
const serverAdapterBaseObjectProp = serverAdapterBaseObject[prop];
|
|
444
444
|
if (serverAdapterBaseObjectProp) {
|
|
445
445
|
if (serverAdapterBaseObjectProp.bind) {
|
|
446
|
-
return
|
|
446
|
+
return function (...args) {
|
|
447
|
+
const returnedVal = serverAdapterBaseObject[prop](...args);
|
|
448
|
+
if (returnedVal === serverAdapterBaseObject) {
|
|
449
|
+
return serverAdapter;
|
|
450
|
+
}
|
|
451
|
+
};
|
|
447
452
|
}
|
|
448
453
|
return serverAdapterBaseObjectProp;
|
|
449
454
|
}
|
|
@@ -452,7 +457,8 @@ function createServerAdapter(serverAdapterBaseObject, options) {
|
|
|
452
457
|
apply(_, __, args) {
|
|
453
458
|
return genericRequestHandler(...args);
|
|
454
459
|
},
|
|
455
|
-
});
|
|
460
|
+
});
|
|
461
|
+
return serverAdapter;
|
|
456
462
|
}
|
|
457
463
|
// from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#copying_accessors
|
|
458
464
|
function completeAssign(target, ...sources) {
|
package/index.mjs
CHANGED
|
@@ -415,7 +415,7 @@ function createServerAdapter(serverAdapterBaseObject, options) {
|
|
|
415
415
|
handleEvent,
|
|
416
416
|
handle: genericRequestHandler,
|
|
417
417
|
};
|
|
418
|
-
|
|
418
|
+
const serverAdapter = new Proxy(genericRequestHandler, {
|
|
419
419
|
// It should have all the attributes of the handler function and the server instance
|
|
420
420
|
has: (_, prop) => {
|
|
421
421
|
return (prop in adapterObj ||
|
|
@@ -441,7 +441,12 @@ function createServerAdapter(serverAdapterBaseObject, options) {
|
|
|
441
441
|
const serverAdapterBaseObjectProp = serverAdapterBaseObject[prop];
|
|
442
442
|
if (serverAdapterBaseObjectProp) {
|
|
443
443
|
if (serverAdapterBaseObjectProp.bind) {
|
|
444
|
-
return
|
|
444
|
+
return function (...args) {
|
|
445
|
+
const returnedVal = serverAdapterBaseObject[prop](...args);
|
|
446
|
+
if (returnedVal === serverAdapterBaseObject) {
|
|
447
|
+
return serverAdapter;
|
|
448
|
+
}
|
|
449
|
+
};
|
|
445
450
|
}
|
|
446
451
|
return serverAdapterBaseObjectProp;
|
|
447
452
|
}
|
|
@@ -450,7 +455,8 @@ function createServerAdapter(serverAdapterBaseObject, options) {
|
|
|
450
455
|
apply(_, __, args) {
|
|
451
456
|
return genericRequestHandler(...args);
|
|
452
457
|
},
|
|
453
|
-
});
|
|
458
|
+
});
|
|
459
|
+
return serverAdapter;
|
|
454
460
|
}
|
|
455
461
|
// from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#copying_accessors
|
|
456
462
|
function completeAssign(target, ...sources) {
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@whatwg-node/server",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.2-alpha-20230301144304-75f91ef",
|
|
4
4
|
"description": "Fetch API compliant HTTP Server adapter",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@whatwg-node/fetch": "
|
|
7
|
+
"@whatwg-node/fetch": "0.8.2-alpha-20230301144304-75f91ef",
|
|
8
8
|
"tslib": "^2.3.1"
|
|
9
9
|
},
|
|
10
10
|
"repository": {
|