@niledatabase/express 5.0.0-alpha.10
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/CHANGELOG.md +36 -0
- package/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/index.d.mts +31 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/jest.config.js +6 -0
- package/package.json +42 -0
- package/src/express.test.ts +54 -0
- package/src/index.ts +146 -0
- package/tsup.config.js +9 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
# [5.0.0-alpha.10](https://github.com/niledatabase/nile-js/compare/v5.0.0-alpha.9...v5.0.0-alpha.10) (2025-06-16)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @niledatabase/express
|
|
9
|
+
|
|
10
|
+
# [5.0.0-alpha.9](https://github.com/niledatabase/nile-js/compare/v5.0.0-alpha.8...v5.0.0-alpha.9) (2025-06-16)
|
|
11
|
+
|
|
12
|
+
**Note:** Version bump only for package @niledatabase/express
|
|
13
|
+
|
|
14
|
+
# [5.0.0-alpha.8](https://github.com/niledatabase/nile-js/compare/v5.0.0-alpha.7...v5.0.0-alpha.8) (2025-06-12)
|
|
15
|
+
|
|
16
|
+
**Note:** Version bump only for package @niledatabase/express
|
|
17
|
+
|
|
18
|
+
# [5.0.0-alpha.7](https://github.com/niledatabase/nile-js/compare/v5.0.0-alpha.6...v5.0.0-alpha.7) (2025-06-11)
|
|
19
|
+
|
|
20
|
+
**Note:** Version bump only for package @niledatabase/express
|
|
21
|
+
|
|
22
|
+
# [5.0.0-alpha.6](https://github.com/niledatabase/nile-js/compare/v5.0.0-alpha.5...v5.0.0-alpha.6) (2025-06-11)
|
|
23
|
+
|
|
24
|
+
### Bug Fixes
|
|
25
|
+
|
|
26
|
+
- **server:** move logging to the config object ([6e3e380](https://github.com/niledatabase/nile-js/commit/6e3e38014f5b9795552af0a7b97e2d3fe6cd1a88))
|
|
27
|
+
|
|
28
|
+
# [5.0.0-alpha.5](https://github.com/niledatabase/nile-js/compare/v5.0.0-alpha.4...v5.0.0-alpha.5) (2025-06-10)
|
|
29
|
+
|
|
30
|
+
**Note:** Version bump only for package @niledatabase/express
|
|
31
|
+
|
|
32
|
+
# [5.0.0-alpha.4](https://github.com/niledatabase/nile-js/compare/v5.0.0-alpha.3...v5.0.0-alpha.4) (2025-06-10)
|
|
33
|
+
|
|
34
|
+
### Features
|
|
35
|
+
|
|
36
|
+
- **server:** invites ([c10f598](https://github.com/niledatabase/nile-js/commit/c10f5980bcbc55ed436ce8cceca4111aa8e6a276))
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 The Nile Platform
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Server, NileConfig } from '@niledatabase/server';
|
|
2
|
+
|
|
3
|
+
declare function cleaner(val: string): string;
|
|
4
|
+
declare function expressPaths(nile: Server): {
|
|
5
|
+
paths: {
|
|
6
|
+
get: string[];
|
|
7
|
+
post: string[];
|
|
8
|
+
put: string[];
|
|
9
|
+
delete: string[];
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
type HandlerConfig = {
|
|
13
|
+
muteResponse?: boolean;
|
|
14
|
+
init?: RequestInit;
|
|
15
|
+
};
|
|
16
|
+
declare function NileExpressHandler(nile: Server, config?: HandlerConfig & NileConfig): Promise<{
|
|
17
|
+
handler: (req: any, res?: any) => Promise<{
|
|
18
|
+
body: string;
|
|
19
|
+
status: number;
|
|
20
|
+
headers: Record<string, string | string[]>;
|
|
21
|
+
response: Response;
|
|
22
|
+
} | null | undefined>;
|
|
23
|
+
paths: {
|
|
24
|
+
get: string[];
|
|
25
|
+
post: string[];
|
|
26
|
+
put: string[];
|
|
27
|
+
delete: string[];
|
|
28
|
+
};
|
|
29
|
+
}>;
|
|
30
|
+
|
|
31
|
+
export { NileExpressHandler, cleaner, expressPaths };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Server, NileConfig } from '@niledatabase/server';
|
|
2
|
+
|
|
3
|
+
declare function cleaner(val: string): string;
|
|
4
|
+
declare function expressPaths(nile: Server): {
|
|
5
|
+
paths: {
|
|
6
|
+
get: string[];
|
|
7
|
+
post: string[];
|
|
8
|
+
put: string[];
|
|
9
|
+
delete: string[];
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
type HandlerConfig = {
|
|
13
|
+
muteResponse?: boolean;
|
|
14
|
+
init?: RequestInit;
|
|
15
|
+
};
|
|
16
|
+
declare function NileExpressHandler(nile: Server, config?: HandlerConfig & NileConfig): Promise<{
|
|
17
|
+
handler: (req: any, res?: any) => Promise<{
|
|
18
|
+
body: string;
|
|
19
|
+
status: number;
|
|
20
|
+
headers: Record<string, string | string[]>;
|
|
21
|
+
response: Response;
|
|
22
|
+
} | null | undefined>;
|
|
23
|
+
paths: {
|
|
24
|
+
get: string[];
|
|
25
|
+
post: string[];
|
|
26
|
+
put: string[];
|
|
27
|
+
delete: string[];
|
|
28
|
+
};
|
|
29
|
+
}>;
|
|
30
|
+
|
|
31
|
+
export { NileExpressHandler, cleaner, expressPaths };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var R=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var P=Object.getOwnPropertyNames;var E=Object.prototype.hasOwnProperty;var T=(n,e)=>{for(var r in e)R(n,r,{get:e[r],enumerable:!0})},S=(n,e,r,d)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of P(e))!E.call(n,i)&&i!==r&&R(n,i,{get:()=>e[i],enumerable:!(d=x(e,i))||d.enumerable});return n};var U=n=>S(R({},"__esModule",{value:!0}),n);var C={};T(C,{NileExpressHandler:()=>j,cleaner:()=>p,expressPaths:()=>b});module.exports=U(C);function p(n){return n.replaceAll(/\{([^}]+)\}/g,":$1")}function b(n){let e=n.getPaths();return{paths:{get:e.get.map(p),post:e.post.map(p),put:e.put.map(p),delete:e.delete.map(p)}}}async function j(n,e){let r=e?.logger?e?.logger("express").error:()=>null;async function d(t,u){let f=new Headers;if(!t||typeof t!="object")return null;if(!("url"in t)||typeof t?.url!="string")return r("A url is necessary for the nile express handler"),null;let h="method"in t&&typeof t.method=="string"?t.method:"GET";"headers"in t&&typeof t.headers=="object"&&t.headers&&"cookie"in t.headers&&typeof t.headers.cookie=="string"&&f.set("cookie",t.headers.cookie);let g={method:h,...e?.init};"body"in t&&(h==="POST"||h==="PUT")&&(f.set("content-type","application/json"),g.body=JSON.stringify(t.body)),g.headers=f;let y=t.protocol+"://"+t.get("host")+t.originalUrl;try{new URL(y)}catch(s){return r("Invalid URL",{url:y,error:s}),null}let w=new Request(y,g),o;try{o=await n.handlers[h](w)}catch(s){r(s)}let a;if(o instanceof Response){try{a=await(await o.clone()).json()}catch{a=await o.text()}let s={};if(o.headers.forEach((c,l)=>{if(!["content-length","transfer-encoding"].includes(l.toLowerCase()))if(s[l]){let m=s[l];Array.isArray(m)?s[l]=[...m,c]:s[l]=[m,c]}else s[l]=c}),e?.muteResponse!==!0){u&&(u.status(o.status).set(s),typeof a=="string"?u.send(a):u.json(a??{}));return}return{body:a,status:o.status,headers:s,response:o}}else{r("Bad response",{response:o});return}}let{paths:i}=b(n);return{handler:d,paths:i}}0&&(module.exports={NileExpressHandler,cleaner,expressPaths});
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { NileConfig, Server } from '@niledatabase/server';\n\nexport function cleaner(val: string) {\n return val.replaceAll(/\\{([^}]+)\\}/g, ':$1');\n}\n\nexport function expressPaths(nile: Server) {\n const nilePaths = nile.getPaths();\n const paths = {\n get: nilePaths.get.map(cleaner),\n post: nilePaths.post.map(cleaner),\n put: nilePaths.put.map(cleaner),\n delete: nilePaths.delete.map(cleaner),\n };\n return {\n paths,\n };\n}\n\ntype HandlerConfig = { muteResponse?: boolean; init?: RequestInit };\nexport async function NileExpressHandler(\n nile: Server,\n config?: HandlerConfig & NileConfig\n) {\n const error = config?.logger ? config?.logger('express').error : () => null;\n async function handler(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n req: any,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n res?: any\n ): Promise<\n | {\n body: string;\n status: number;\n headers: Record<string, string | string[]>;\n response: Response;\n }\n | null\n | undefined\n > {\n const headers = new Headers();\n if (!req || typeof req !== 'object') {\n return null;\n }\n if (!('url' in req) || typeof req?.url !== 'string') {\n error('A url is necessary for the nile express handler');\n return null;\n }\n const method =\n 'method' in req && typeof req.method === 'string' ? req.method : 'GET';\n\n if (\n 'headers' in req &&\n typeof req.headers === 'object' &&\n req.headers &&\n 'cookie' in req.headers &&\n typeof req.headers.cookie === 'string'\n ) {\n headers.set('cookie', req.headers.cookie);\n }\n const _init: RequestInit = { method, ...config?.init };\n\n if ('body' in req) {\n if (method === 'POST' || method === 'PUT') {\n headers.set('content-type', 'application/json');\n _init.body = JSON.stringify(req.body);\n }\n }\n\n _init.headers = headers;\n\n const reqUrl = req.protocol + '://' + req.get('host') + req.originalUrl;\n\n // be sure its a valid url\n try {\n new URL(reqUrl);\n } catch (e) {\n error('Invalid URL', {\n url: reqUrl,\n error: e,\n });\n return null;\n }\n const proxyRequest = new Request(reqUrl, _init);\n let response;\n try {\n response = await nile.handlers[\n method as 'GET' | 'POST' | 'PUT' | 'DELETE'\n ](proxyRequest);\n } catch (e) {\n error(e);\n }\n\n let body;\n\n if (response instanceof Response) {\n try {\n const tryJson = await response.clone();\n body = await tryJson.json();\n } catch (e) {\n body = await response.text();\n }\n const newHeaders: Record<string, string | string[]> = {};\n response.headers.forEach((value, key) => {\n if (\n !['content-length', 'transfer-encoding'].includes(key.toLowerCase())\n ) {\n if (newHeaders[key]) {\n const prev = newHeaders[key];\n if (Array.isArray(prev)) {\n newHeaders[key] = [...prev, value];\n } else {\n newHeaders[key] = [prev, value];\n }\n } else {\n newHeaders[key] = value;\n }\n }\n });\n\n if (config?.muteResponse !== true) {\n if (res) {\n res.status(response.status).set(newHeaders);\n if (typeof body === 'string') {\n res.send(body);\n } else {\n res.json(body ?? {});\n }\n }\n return;\n }\n\n return {\n body,\n status: response.status,\n headers: newHeaders,\n response,\n };\n } else {\n error('Bad response', { response });\n return;\n }\n }\n const { paths } = expressPaths(nile);\n return { handler, paths };\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,wBAAAE,EAAA,YAAAC,EAAA,iBAAAC,IAAA,eAAAC,EAAAL,GAEO,SAASG,EAAQG,EAAa,CACnC,OAAOA,EAAI,WAAW,eAAgB,KAAK,CAC7C,CAEO,SAASF,EAAaG,EAAc,CACzC,IAAMC,EAAYD,EAAK,SAAS,EAOhC,MAAO,CACL,MAPY,CACZ,IAAKC,EAAU,IAAI,IAAIL,CAAO,EAC9B,KAAMK,EAAU,KAAK,IAAIL,CAAO,EAChC,IAAKK,EAAU,IAAI,IAAIL,CAAO,EAC9B,OAAQK,EAAU,OAAO,IAAIL,CAAO,CACtC,CAGA,CACF,CAGA,eAAsBD,EACpBK,EACAE,EACA,CACA,IAAMC,EAAQD,GAAQ,OAASA,GAAQ,OAAO,SAAS,EAAE,MAAQ,IAAM,KACvE,eAAeE,EAEbC,EAEAC,EAUA,CACA,IAAMC,EAAU,IAAI,QACpB,GAAI,CAACF,GAAO,OAAOA,GAAQ,SACzB,OAAO,KAET,GAAI,EAAE,QAASA,IAAQ,OAAOA,GAAK,KAAQ,SACzC,OAAAF,EAAM,iDAAiD,EAChD,KAET,IAAMK,EACJ,WAAYH,GAAO,OAAOA,EAAI,QAAW,SAAWA,EAAI,OAAS,MAGjE,YAAaA,GACb,OAAOA,EAAI,SAAY,UACvBA,EAAI,SACJ,WAAYA,EAAI,SAChB,OAAOA,EAAI,QAAQ,QAAW,UAE9BE,EAAQ,IAAI,SAAUF,EAAI,QAAQ,MAAM,EAE1C,IAAMI,EAAqB,CAAE,OAAAD,EAAQ,GAAGN,GAAQ,IAAK,EAEjD,SAAUG,IACRG,IAAW,QAAUA,IAAW,SAClCD,EAAQ,IAAI,eAAgB,kBAAkB,EAC9CE,EAAM,KAAO,KAAK,UAAUJ,EAAI,IAAI,G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ClC,MACF,CACF,CACA,GAAM,CAAE,MAAAM,CAAM,EAAItB,EAAaG,CAAI,EACnC,MAAO,CAAE,QAAAI,EAAS,MAAAe,CAAM,CAC1B","names":["index_exports","__export","NileExpressHandler","cleaner","expressPaths","__toCommonJS","val","nile","nilePaths","config","error","handler","req","res","headers","method","_init","reqUrl","e","proxyRequest","response","body","newHeaders","value","key","prev","paths"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
function d(r){return r.replaceAll(/\{([^}]+)\}/g,":$1")}function b(r){let n=r.getPaths();return{paths:{get:n.get.map(d),post:n.post.map(d),put:n.put.map(d),delete:n.delete.map(d)}}}async function w(r,n){let a=n?.logger?n?.logger("express").error:()=>null;async function y(e,c){let u=new Headers;if(!e||typeof e!="object")return null;if(!("url"in e)||typeof e?.url!="string")return a("A url is necessary for the nile express handler"),null;let p="method"in e&&typeof e.method=="string"?e.method:"GET";"headers"in e&&typeof e.headers=="object"&&e.headers&&"cookie"in e.headers&&typeof e.headers.cookie=="string"&&u.set("cookie",e.headers.cookie);let h={method:p,...n?.init};"body"in e&&(p==="POST"||p==="PUT")&&(u.set("content-type","application/json"),h.body=JSON.stringify(e.body)),h.headers=u;let f=e.protocol+"://"+e.get("host")+e.originalUrl;try{new URL(f)}catch(t){return a("Invalid URL",{url:f,error:t}),null}let R=new Request(f,h),s;try{s=await r.handlers[p](R)}catch(t){a(t)}let o;if(s instanceof Response){try{o=await(await s.clone()).json()}catch{o=await s.text()}let t={};if(s.headers.forEach((l,i)=>{if(!["content-length","transfer-encoding"].includes(i.toLowerCase()))if(t[i]){let g=t[i];Array.isArray(g)?t[i]=[...g,l]:t[i]=[g,l]}else t[i]=l}),n?.muteResponse!==!0){c&&(c.status(s.status).set(t),typeof o=="string"?c.send(o):c.json(o??{}));return}return{body:o,status:s.status,headers:t,response:s}}else{a("Bad response",{response:s});return}}let{paths:m}=b(r);return{handler:y,paths:m}}export{w as NileExpressHandler,d as cleaner,b as expressPaths};
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { NileConfig, Server } from '@niledatabase/server';\n\nexport function cleaner(val: string) {\n return val.replaceAll(/\\{([^}]+)\\}/g, ':$1');\n}\n\nexport function expressPaths(nile: Server) {\n const nilePaths = nile.getPaths();\n const paths = {\n get: nilePaths.get.map(cleaner),\n post: nilePaths.post.map(cleaner),\n put: nilePaths.put.map(cleaner),\n delete: nilePaths.delete.map(cleaner),\n };\n return {\n paths,\n };\n}\n\ntype HandlerConfig = { muteResponse?: boolean; init?: RequestInit };\nexport async function NileExpressHandler(\n nile: Server,\n config?: HandlerConfig & NileConfig\n) {\n const error = config?.logger ? config?.logger('express').error : () => null;\n async function handler(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n req: any,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n res?: any\n ): Promise<\n | {\n body: string;\n status: number;\n headers: Record<string, string | string[]>;\n response: Response;\n }\n | null\n | undefined\n > {\n const headers = new Headers();\n if (!req || typeof req !== 'object') {\n return null;\n }\n if (!('url' in req) || typeof req?.url !== 'string') {\n error('A url is necessary for the nile express handler');\n return null;\n }\n const method =\n 'method' in req && typeof req.method === 'string' ? req.method : 'GET';\n\n if (\n 'headers' in req &&\n typeof req.headers === 'object' &&\n req.headers &&\n 'cookie' in req.headers &&\n typeof req.headers.cookie === 'string'\n ) {\n headers.set('cookie', req.headers.cookie);\n }\n const _init: RequestInit = { method, ...config?.init };\n\n if ('body' in req) {\n if (method === 'POST' || method === 'PUT') {\n headers.set('content-type', 'application/json');\n _init.body = JSON.stringify(req.body);\n }\n }\n\n _init.headers = headers;\n\n const reqUrl = req.protocol + '://' + req.get('host') + req.originalUrl;\n\n // be sure its a valid url\n try {\n new URL(reqUrl);\n } catch (e) {\n error('Invalid URL', {\n url: reqUrl,\n error: e,\n });\n return null;\n }\n const proxyRequest = new Request(reqUrl, _init);\n let response;\n try {\n response = await nile.handlers[\n method as 'GET' | 'POST' | 'PUT' | 'DELETE'\n ](proxyRequest);\n } catch (e) {\n error(e);\n }\n\n let body;\n\n if (response instanceof Response) {\n try {\n const tryJson = await response.clone();\n body = await tryJson.json();\n } catch (e) {\n body = await response.text();\n }\n const newHeaders: Record<string, string | string[]> = {};\n response.headers.forEach((value, key) => {\n if (\n !['content-length', 'transfer-encoding'].includes(key.toLowerCase())\n ) {\n if (newHeaders[key]) {\n const prev = newHeaders[key];\n if (Array.isArray(prev)) {\n newHeaders[key] = [...prev, value];\n } else {\n newHeaders[key] = [prev, value];\n }\n } else {\n newHeaders[key] = value;\n }\n }\n });\n\n if (config?.muteResponse !== true) {\n if (res) {\n res.status(response.status).set(newHeaders);\n if (typeof body === 'string') {\n res.send(body);\n } else {\n res.json(body ?? {});\n }\n }\n return;\n }\n\n return {\n body,\n status: response.status,\n headers: newHeaders,\n response,\n };\n } else {\n error('Bad response', { response });\n return;\n }\n }\n const { paths } = expressPaths(nile);\n return { handler, paths };\n}\n"],"mappings":"AAEO,SAASA,EAAQC,EAAa,CACnC,OAAOA,EAAI,WAAW,eAAgB,KAAK,CAC7C,CAEO,SAASC,EAAaC,EAAc,CACzC,IAAMC,EAAYD,EAAK,SAAS,EAOhC,MAAO,CACL,MAPY,CACZ,IAAKC,EAAU,IAAI,IAAIJ,CAAO,EAC9B,KAAMI,EAAU,KAAK,IAAIJ,CAAO,EAChC,IAAKI,EAAU,IAAI,IAAIJ,CAAO,EAC9B,OAAQI,EAAU,OAAO,IAAIJ,CAAO,CACtC,CAGA,CACF,CAGA,eAAsBK,EACpBF,EACAG,EACA,CACA,IAAMC,EAAQD,GAAQ,OAASA,GAAQ,OAAO,SAAS,EAAE,MAAQ,IAAM,KACvE,eAAeE,EAEbC,EAEAC,EAUA,CACA,IAAMC,EAAU,IAAI,QACpB,GAAI,CAACF,GAAO,OAAOA,GAAQ,SACzB,OAAO,KAET,GAAI,EAAE,QAASA,IA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,CAAI,EACpBH,EAAWE,CAAG,EAAI,CAAC,GAAGC,EAAMF,CAAK,EAEjCD,EAAWE,CAAG,EAAI,CAACC,EAAMF,CAAK,CAElC,MACED,EAAWE,CAAG,EAAID,CAGxB,CAAC,EAEGd,GAAQ,eAAiB,GAAM,CAC7BI,IACFA,EAAI,OAAOO,EAAS,MAAM,EAAE,IAAIE,CAAU,EACtC,OAAOD,GAAS,SAClBR,EAAI,KAAKQ,CAAI,EAEbR,EAAI,KAAKQ,GAAQ,CAAC,CAAC,GAGvB,MACF,CAEA,MAAO,CACL,KAAAA,EACA,OAAQD,EAAS,OACjB,QAASE,EACT,SAAAF,CACF,CACF,KAAO,CACLV,EAAM,eAAgB,CAAE,SAAAU,CAAS,CAAC,EAClC,MACF,CACF,CACA,GAAM,CAAE,MAAAM,CAAM,EAAIrB,EAAaC,CAAI,EACnC,MAAO,CAAE,QAAAK,EAAS,MAAAe,CAAM,CAC1B","names":["cleaner","val","expressPaths","nile","nilePaths","NileExpressHandler","config","error","handler","req","res","headers","method","_init","reqUrl","e","proxyRequest","response","body","newHeaders","value","key","prev","paths"]}
|
package/jest.config.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@niledatabase/express",
|
|
3
|
+
"version": "5.0.0-alpha.10",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"require": "./dist/index.js",
|
|
11
|
+
"import": "./dist/index.mjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"prettier": {
|
|
15
|
+
"printWidth": 80,
|
|
16
|
+
"semi": true,
|
|
17
|
+
"singleQuote": true,
|
|
18
|
+
"trailingComma": "es5"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsup src/index.ts",
|
|
22
|
+
"test": "jest"
|
|
23
|
+
},
|
|
24
|
+
"author": "jrea",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "https://github.com/niledatabase/nile-js.git",
|
|
28
|
+
"directory": "packages/express"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"@niledatabase/server": "*"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"jest": "^29.7.0",
|
|
38
|
+
"ts-jest": "^29.3.4",
|
|
39
|
+
"tsup": "^8.5.0"
|
|
40
|
+
},
|
|
41
|
+
"gitHead": "d81efc26168cb58f8d8f809898afc0ffc030c030"
|
|
42
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Server } from '@niledatabase/server';
|
|
2
|
+
|
|
3
|
+
import { expressPaths } from '.';
|
|
4
|
+
|
|
5
|
+
describe('express', () => {
|
|
6
|
+
it('cleans express paths', () => {
|
|
7
|
+
const nile = new Server({
|
|
8
|
+
apiUrl: 'http://localhost:3000',
|
|
9
|
+
user: '123',
|
|
10
|
+
password: '123',
|
|
11
|
+
databaseName: '123',
|
|
12
|
+
});
|
|
13
|
+
const { paths } = expressPaths(nile);
|
|
14
|
+
expect(Object.keys(paths)).toEqual(['get', 'post', 'put', 'delete']);
|
|
15
|
+
expect(paths.delete).toEqual([
|
|
16
|
+
'/api/tenants/:tenantId/users/:userId',
|
|
17
|
+
'/api/tenants/:tenantId',
|
|
18
|
+
]);
|
|
19
|
+
expect(paths.post).toEqual([
|
|
20
|
+
'/api/tenants/:tenantId/users',
|
|
21
|
+
'/api/signup',
|
|
22
|
+
'/api/users',
|
|
23
|
+
'/api/tenants',
|
|
24
|
+
'/api/auth/session',
|
|
25
|
+
'/api/auth/signin/:provider',
|
|
26
|
+
'/api/auth/reset-password',
|
|
27
|
+
'/api/auth/providers',
|
|
28
|
+
'/api/auth/csrf',
|
|
29
|
+
'/api/auth/callback/:provider',
|
|
30
|
+
'/api/auth/signout',
|
|
31
|
+
]);
|
|
32
|
+
expect(paths.put).toEqual([
|
|
33
|
+
'/api/tenants/:tenantId/users',
|
|
34
|
+
'/api/users',
|
|
35
|
+
'/api/tenants/:tenantId',
|
|
36
|
+
'/api/auth/reset-password',
|
|
37
|
+
]);
|
|
38
|
+
expect(paths.get).toEqual([
|
|
39
|
+
'/api/me',
|
|
40
|
+
'/api/tenants/:tenantId/users',
|
|
41
|
+
'/api/tenants',
|
|
42
|
+
'/api/tenants/:tenantId',
|
|
43
|
+
'/api/auth/session',
|
|
44
|
+
'/api/auth/signin',
|
|
45
|
+
'/api/auth/providers',
|
|
46
|
+
'/api/auth/csrf',
|
|
47
|
+
'/api/auth/reset-password',
|
|
48
|
+
'/api/auth/callback',
|
|
49
|
+
'/api/auth/signout',
|
|
50
|
+
'/api/auth/verify-request',
|
|
51
|
+
'/api/auth/error',
|
|
52
|
+
]);
|
|
53
|
+
});
|
|
54
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { NileConfig, Server } from '@niledatabase/server';
|
|
2
|
+
|
|
3
|
+
export function cleaner(val: string) {
|
|
4
|
+
return val.replaceAll(/\{([^}]+)\}/g, ':$1');
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function expressPaths(nile: Server) {
|
|
8
|
+
const nilePaths = nile.getPaths();
|
|
9
|
+
const paths = {
|
|
10
|
+
get: nilePaths.get.map(cleaner),
|
|
11
|
+
post: nilePaths.post.map(cleaner),
|
|
12
|
+
put: nilePaths.put.map(cleaner),
|
|
13
|
+
delete: nilePaths.delete.map(cleaner),
|
|
14
|
+
};
|
|
15
|
+
return {
|
|
16
|
+
paths,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
type HandlerConfig = { muteResponse?: boolean; init?: RequestInit };
|
|
21
|
+
export async function NileExpressHandler(
|
|
22
|
+
nile: Server,
|
|
23
|
+
config?: HandlerConfig & NileConfig
|
|
24
|
+
) {
|
|
25
|
+
const error = config?.logger ? config?.logger('express').error : () => null;
|
|
26
|
+
async function handler(
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
28
|
+
req: any,
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
30
|
+
res?: any
|
|
31
|
+
): Promise<
|
|
32
|
+
| {
|
|
33
|
+
body: string;
|
|
34
|
+
status: number;
|
|
35
|
+
headers: Record<string, string | string[]>;
|
|
36
|
+
response: Response;
|
|
37
|
+
}
|
|
38
|
+
| null
|
|
39
|
+
| undefined
|
|
40
|
+
> {
|
|
41
|
+
const headers = new Headers();
|
|
42
|
+
if (!req || typeof req !== 'object') {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
if (!('url' in req) || typeof req?.url !== 'string') {
|
|
46
|
+
error('A url is necessary for the nile express handler');
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
const method =
|
|
50
|
+
'method' in req && typeof req.method === 'string' ? req.method : 'GET';
|
|
51
|
+
|
|
52
|
+
if (
|
|
53
|
+
'headers' in req &&
|
|
54
|
+
typeof req.headers === 'object' &&
|
|
55
|
+
req.headers &&
|
|
56
|
+
'cookie' in req.headers &&
|
|
57
|
+
typeof req.headers.cookie === 'string'
|
|
58
|
+
) {
|
|
59
|
+
headers.set('cookie', req.headers.cookie);
|
|
60
|
+
}
|
|
61
|
+
const _init: RequestInit = { method, ...config?.init };
|
|
62
|
+
|
|
63
|
+
if ('body' in req) {
|
|
64
|
+
if (method === 'POST' || method === 'PUT') {
|
|
65
|
+
headers.set('content-type', 'application/json');
|
|
66
|
+
_init.body = JSON.stringify(req.body);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
_init.headers = headers;
|
|
71
|
+
|
|
72
|
+
const reqUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
|
|
73
|
+
|
|
74
|
+
// be sure its a valid url
|
|
75
|
+
try {
|
|
76
|
+
new URL(reqUrl);
|
|
77
|
+
} catch (e) {
|
|
78
|
+
error('Invalid URL', {
|
|
79
|
+
url: reqUrl,
|
|
80
|
+
error: e,
|
|
81
|
+
});
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
const proxyRequest = new Request(reqUrl, _init);
|
|
85
|
+
let response;
|
|
86
|
+
try {
|
|
87
|
+
response = await nile.handlers[
|
|
88
|
+
method as 'GET' | 'POST' | 'PUT' | 'DELETE'
|
|
89
|
+
](proxyRequest);
|
|
90
|
+
} catch (e) {
|
|
91
|
+
error(e);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
let body;
|
|
95
|
+
|
|
96
|
+
if (response instanceof Response) {
|
|
97
|
+
try {
|
|
98
|
+
const tryJson = await response.clone();
|
|
99
|
+
body = await tryJson.json();
|
|
100
|
+
} catch (e) {
|
|
101
|
+
body = await response.text();
|
|
102
|
+
}
|
|
103
|
+
const newHeaders: Record<string, string | string[]> = {};
|
|
104
|
+
response.headers.forEach((value, key) => {
|
|
105
|
+
if (
|
|
106
|
+
!['content-length', 'transfer-encoding'].includes(key.toLowerCase())
|
|
107
|
+
) {
|
|
108
|
+
if (newHeaders[key]) {
|
|
109
|
+
const prev = newHeaders[key];
|
|
110
|
+
if (Array.isArray(prev)) {
|
|
111
|
+
newHeaders[key] = [...prev, value];
|
|
112
|
+
} else {
|
|
113
|
+
newHeaders[key] = [prev, value];
|
|
114
|
+
}
|
|
115
|
+
} else {
|
|
116
|
+
newHeaders[key] = value;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
if (config?.muteResponse !== true) {
|
|
122
|
+
if (res) {
|
|
123
|
+
res.status(response.status).set(newHeaders);
|
|
124
|
+
if (typeof body === 'string') {
|
|
125
|
+
res.send(body);
|
|
126
|
+
} else {
|
|
127
|
+
res.json(body ?? {});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return {
|
|
134
|
+
body,
|
|
135
|
+
status: response.status,
|
|
136
|
+
headers: newHeaders,
|
|
137
|
+
response,
|
|
138
|
+
};
|
|
139
|
+
} else {
|
|
140
|
+
error('Bad response', { response });
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
const { paths } = expressPaths(nile);
|
|
145
|
+
return { handler, paths };
|
|
146
|
+
}
|