@server/next 0.22.1 → 0.23.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@server/next",
3
- "version": "0.22.1",
3
+ "version": "0.23.1",
4
4
  "description": "A fully-fledged web server with routing, file uploads, sessions, static files, schema validation, websockets, testing, etc.",
5
5
  "homepage": "https://server-js.com/",
6
6
  "repository": "https://github.com/franciscop/server-next.git",
@@ -20,7 +20,7 @@
20
20
  "scripts": {
21
21
  "start": "bun test --watch",
22
22
  "lint": "npx @biomejs/biome lint ./src --skip=lint/style/noParameterAssign",
23
- "test": "bun test",
23
+ "test": "bun test && check-dts ./src/**",
24
24
  "test:jest": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
25
25
  },
26
26
  "keywords": [
@@ -29,15 +29,15 @@
29
29
  "server.js"
30
30
  ],
31
31
  "type": "module",
32
- "types": "index.d.ts",
33
32
  "main": "src/index.js",
33
+ "types": "src/index.d.ts",
34
34
  "files": [
35
35
  "src/",
36
- "index.d.ts",
37
36
  "jsx-dev-runtime.js"
38
37
  ],
39
38
  "devDependencies": {
40
39
  "argon2": "^0.40.3",
40
+ "check-dts": "^0.8.2",
41
41
  "jest": "^29.7.0",
42
42
  "polystore": "^0.14.1"
43
43
  },
@@ -45,8 +45,7 @@ const randomId = (size = 16) => {
45
45
  return id;
46
46
  };
47
47
 
48
- export default function createId(source) {
49
- const size = 16;
48
+ export default function createId(source, size = 16) {
50
49
  if (source) return hash(source, size);
51
50
  return randomId(size);
52
51
  }
@@ -16,6 +16,7 @@ const altAttrs = {
16
16
  // "" and 0 are valid children, false and null and undefined are not
17
17
  const isValidChild = (child) => child || child === "" || child === 0;
18
18
 
19
+ const escapeCSS = (value) => String(value).replace(/[<>&"'`]/g, "\\$&");
19
20
  const minifyCss = (str) =>
20
21
  str
21
22
  .replace(/\s+/g, " ")
@@ -42,7 +43,7 @@ export const jsx = (tag, { children, ...props }) => {
42
43
  children = () => src;
43
44
  }
44
45
  if (tag === "style" && children && typeof children === "string") {
45
- const src = minifyCss(children);
46
+ const src = minifyCss(escapeCSS(children));
46
47
  children = () => src;
47
48
  }
48
49
  if (props.dangerouslySetInnerHTML)
package/src/index.d.ts ADDED
@@ -0,0 +1,152 @@
1
+ type Method =
2
+ | "socket"
3
+ | "get"
4
+ | "head"
5
+ | "post"
6
+ | "put"
7
+ | "patch"
8
+ | "delete"
9
+ | "options";
10
+
11
+ type Store = {
12
+ get: (key: string) => Promise<any>;
13
+ set: (
14
+ key: string,
15
+ value: any,
16
+ { expires }?: { expires?: string | number },
17
+ ) => Promise<any>;
18
+ };
19
+
20
+ type Bucket = any;
21
+
22
+ type Auth = {
23
+ type: "cookie" | "token";
24
+ provider: "github" | "email";
25
+ };
26
+ type AuthString = `${Auth["type"]}:${Auth["provider"]}`;
27
+
28
+ type Domain = `https://${string}/`;
29
+ type Origin = boolean | "*" | Domain | Domain[];
30
+ type Cors = {
31
+ origin: Origin;
32
+ methods: string;
33
+ headers: string;
34
+ credentials?: boolean;
35
+ };
36
+
37
+ type ServerOptions = {
38
+ port?: number;
39
+ views?: string | Bucket;
40
+ public?: string | Bucket;
41
+ uploads?: string | Bucket;
42
+ cors?: boolean | Origin | Cors;
43
+ auth?: AuthString | Auth;
44
+ store?: Store;
45
+ };
46
+
47
+ type ExtractPathParams<Path extends string> =
48
+ Path extends `${string}:${infer Param}/${infer Rest}`
49
+ ? Param | ExtractPathParams<`/${Rest}`>
50
+ : Path extends `${string}:${infer Param}`
51
+ ? Param
52
+ : never;
53
+
54
+ type ParamsToObject<Params extends string> = {
55
+ [K in Params as K extends `${infer Key}?`
56
+ ? Key
57
+ : K]: K extends `${infer Key}?` ? string | undefined : string;
58
+ };
59
+
60
+ type PathToParams<Path extends string> = ParamsToObject<
61
+ ExtractPathParams<Path>
62
+ >;
63
+
64
+ type Simplify<T> = T extends object ? { [K in keyof T]: T[K] } : T;
65
+
66
+ type Context<Path extends string = string> = {
67
+ method: Method;
68
+ headers: { [key: string]: string | string[] };
69
+ cookies: { [key: string]: any };
70
+ body?: any;
71
+ url: URL & {
72
+ params: Simplify<PathToParams<Path>>; // Simplify here
73
+ query: { [key: string]: string };
74
+ };
75
+ options: ServerOptions;
76
+ };
77
+
78
+ type Body = string;
79
+
80
+ type ContentType = "application/json" | "text/plain" | (string & {});
81
+
82
+ // (src: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers)
83
+ type Headers = {
84
+ "cache-control"?: string;
85
+ "content-type"?: ContentType;
86
+ "set-cookie"?: string;
87
+ "content-length"?: string;
88
+ server?: string;
89
+ location?: string;
90
+
91
+ [key: string]: string | undefined;
92
+ };
93
+
94
+ type InlineReply =
95
+ | Response
96
+ | { body: Body; headers?: Headers }
97
+ | string
98
+ | number
99
+ | void;
100
+
101
+ type Middleware<Path extends string = string> = (
102
+ ctx: Context<Path>,
103
+ ) => InlineReply;
104
+
105
+ declare interface Router {
106
+ get<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
107
+ head<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
108
+ post<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
109
+ put<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
110
+ patch<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
111
+ del<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
112
+ options<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
113
+ }
114
+
115
+ declare interface Server {
116
+ /**
117
+ * Launch the server with the optional configuration:
118
+ *
119
+ * ```js
120
+ * export default server({
121
+ * port: 3000,
122
+ * public: './public',
123
+ * store: kv(new Map()),
124
+ * })
125
+ * ```
126
+ *
127
+ * **[→ Getting Started](https://react-test.dev/documentation#attr)**
128
+ *
129
+ * **[→ Options Docs](https://react-test.dev/documentation#attr)**
130
+ */
131
+ (options?: ServerOptions): this;
132
+
133
+ socket(path: string, ...middle: Middleware[]): this;
134
+
135
+ get<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
136
+ head<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
137
+ post<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
138
+ put<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
139
+ patch<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
140
+ del<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
141
+ options<Path extends string>(path: Path, ...middle: Middleware<Path>[]): this;
142
+
143
+ use(...middle: Middleware[]): this;
144
+ router(router: Router): this;
145
+ }
146
+
147
+ type headers = (obj?: Headers) => any;
148
+
149
+ declare const server: Server;
150
+ export const router: Router;
151
+ export const headers: headers;
152
+ export default server;
package/src/index.js CHANGED
@@ -21,7 +21,6 @@ export { default as ServerError } from "./ServerError.js";
21
21
  // Allow to create a sub-router
22
22
  export { default as router } from "./router.js";
23
23
 
24
- // #region server()
25
24
  export default function server(options = {}) {
26
25
  // Make it so that the exported one is a prototype of function()
27
26
  if (!(this instanceof server)) {
@@ -0,0 +1,28 @@
1
+ import server, { headers, router } from ".";
2
+
3
+ const users = router
4
+ .post("/users", (ctx) => {
5
+ console.log(ctx.url.pathname);
6
+ console.log(ctx.url.query);
7
+ console.log(ctx.url.params); // No .id
8
+ })
9
+ .get("/users/:id?", (ctx) => {
10
+ console.log(ctx.url.params.id);
11
+ })
12
+ .del("/users/:id", (ctx) => {
13
+ console.log(ctx.url.params.id);
14
+ });
15
+
16
+ server()
17
+ .get("/", (ctx) => {
18
+ console.log(ctx.method);
19
+ console.log(ctx.url);
20
+ console.log(ctx.headers);
21
+ console.log(ctx.cookies);
22
+ console.log(ctx.body);
23
+ return headers();
24
+ })
25
+ .router(users)
26
+ .post("/", () => {
27
+ return 201;
28
+ });
@@ -1,6 +1,4 @@
1
- // import { Readable } from "node:stream";
2
-
3
- import { cors, createCookies, createId } from "./helpers/index.js";
1
+ import { cors, createId } from "./helpers/index.js";
4
2
  import { json } from "./reply.js";
5
3
  import ServerError from "./ServerError.js";
6
4
 
package/index.d.ts DELETED
@@ -1,84 +0,0 @@
1
- type Bucket = {};
2
-
3
- type Method =
4
- | "socket"
5
- | "get"
6
- | "head"
7
- | "post"
8
- | "put"
9
- | "patch"
10
- | "delete"
11
- | "options";
12
-
13
- type ServerOptions = {
14
- port?: number;
15
- views?: string | Bucket;
16
- public?: string | Bucket;
17
- uploads?: string | Bucket;
18
- auth?: string | { type: string; provider: string };
19
- };
20
-
21
- type Context = {
22
- method: Method;
23
- headers: { [key: string]: string | string[] };
24
- cookies: { [key: string]: any };
25
- body?: any;
26
- url: URL & { params: {}; query: {} };
27
- options: ServerOptions;
28
- };
29
-
30
- type Body = string;
31
-
32
- type ContentType = "application/json" | "text/plain" | (string & {});
33
-
34
- // (src: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers)
35
- type Headers = {
36
- "Cache-Control"?: string;
37
- "Content-Type"?: ContentType;
38
- Server?: string;
39
- "Set-Cookie"?: string;
40
- "Content-Length"?: string;
41
- Location?: string;
42
-
43
- "cache-control"?: string;
44
- "content-type"?: ContentType;
45
- server?: string;
46
- "set-cookie"?: string;
47
- "content-length"?: string;
48
- location?: string;
49
-
50
- [key: string]: string | undefined;
51
- };
52
-
53
- type InlineReply =
54
- | Response
55
- | { body: Body; headers?: Headers }
56
- | string
57
- | number
58
- | void;
59
-
60
- type Middleware = (ctx: Context) => InlineReply;
61
-
62
- type Router = {};
63
-
64
- declare interface Server {
65
- (options?: ServerOptions): this;
66
-
67
- socket(path: string, ...middleware: Middleware[]): this;
68
- get(path: string, ...middleware: Middleware[]): this;
69
- head(path: string, ...middleware: Middleware[]): this;
70
- post(path: string, ...middleware: Middleware[]): this;
71
- put(path: string, ...middleware: Middleware[]): this;
72
- patch(path: string, ...middleware: Middleware[]): this;
73
- del(path: string, ...middleware: Middleware[]): this;
74
- options(path: string, ...middleware: Middleware[]): this;
75
-
76
- use(...middleware: Middleware[]): this;
77
- router(router: Router): this;
78
- }
79
-
80
- type headers = (obj?: Headers) => any;
81
-
82
- declare const server: Server;
83
- export const headers: headers;
84
- export default server;