@stonyx/rest-server 0.2.1-alpha.34 → 0.2.1-alpha.36

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 CHANGED
@@ -15,11 +15,36 @@ REST server module for the [Stonyx framework](https://github.com/abofs/stonyx),
15
15
  * **Singleton server:** Only one RestServer instance runs, preventing accidental multiple listeners.
16
16
  * **Framework integration:** Works seamlessly as part of the Stonyx framework—auto-initialized with the rest of your modules.
17
17
 
18
- ## Usage reference
18
+ ## Exports
19
19
 
20
- The sample request classes in [`test/sample/`](https://github.com/abofs/stonyx-rest-server/tree/dev/test/sample) are the canonical usage reference for this module; the integration tests run against them. They are not included in the npm package.
20
+ | Import path | What it exports |
21
+ | :--- | :--- |
22
+ | `@stonyx/rest-server` | `RestServer` (default), `Request` (named); types `RouteHandlers`, `RequestHandler`, `AuthHandler`, `RequestState` |
21
23
 
22
- The package has a single export, `@stonyx/rest-server` (the `RestServer` default export and the named `Request` export). There are no subpath exports.
24
+ There are no subpath exports.
25
+
26
+ ## Samples
27
+
28
+ The request classes in [`test/sample/requests/`](https://github.com/abofs/stonyx-rest-server/tree/dev/test/sample/requests) are the canonical usage reference. They import only `@stonyx/rest-server`, so you can copy them into your app's `requests/` directory as TypeScript, or as JavaScript by deleting the type annotations. [`test/integration/sample-patterns-test.ts`](https://github.com/abofs/stonyx-rest-server/blob/dev/test/integration/sample-patterns-test.ts) exercises each one over HTTP, and [`test/unit/sample-imports-test.ts`](https://github.com/abofs/stonyx-rest-server/blob/dev/test/unit/sample-imports-test.ts) fails if a sample imports anything a consumer cannot resolve. They are not included in the npm package.
29
+
30
+ | How do I… | Sample |
31
+ | :--- | :--- |
32
+ | Run the same middleware (a version gate, say) in front of many routes, and return my own status and JSON body from it | [`base/api-request.ts`](https://github.com/abofs/stonyx-rest-server/blob/dev/test/sample/requests/base/api-request.ts): async middleware added to `this.expressInstance` in the constructor of a base class. Subclasses inherit it. There is no global request hook; this is the pattern |
33
+ | Share one `auth` hook across request classes | [`base/api-request.ts`](https://github.com/abofs/stonyx-rest-server/blob/dev/test/sample/requests/base/api-request.ts), extended by [`accounts.ts`](https://github.com/abofs/stonyx-rest-server/blob/dev/test/sample/requests/accounts.ts). Keep base classes in a subdirectory: route discovery does not recurse, so they are never mounted |
34
+ | Do async work (a lookup) before a handler, and stop the request if it fails | [`accounts.ts`](https://github.com/abofs/stonyx-rest-server/blob/dev/test/sample/requests/accounts.ts): handler-array middleware is awaited; return a status to stop, or set `state` and return `undefined` to continue |
35
+ | Set response headers, or a status other than 200 with a JSON body | [`widgets.ts`](https://github.com/abofs/stonyx-rest-server/blob/dev/test/sample/requests/widgets.ts): `request.res` in a handler for one route, `this.expressInstance` middleware for every route in the class |
36
+ | Use middleware, URL params and `this` in handlers | [`public.ts`](https://github.com/abofs/stonyx-rest-server/blob/dev/test/sample/requests/public.ts) |
37
+
38
+ Two contracts the samples rely on, both pinned by `sample-patterns-test.ts`:
39
+
40
+ * `auth(request, state)` is synchronous. A returned Promise is not awaited; the request fails with **500** whatever the Promise resolves to. Do async checks in middleware.
41
+ * The last function of a route is called without binding `this`. Use an arrow function (as the samples do) to reach the class instance; a method reference fails with 500. Middleware functions are bound.
42
+
43
+ `private.ts` carries one deliberately invalid entry (`someInvalidMethod`) that the suite uses to check unknown methods are skipped. Do not copy it.
44
+
45
+ ## Security notes
46
+
47
+ [`docs/security.md`](https://github.com/abofs/stonyx-rest-server/blob/dev/docs/security.md) covers the security-relevant behaviour: what an `auth` return value allows (any falsy value, including `0`, lets the request through; a Promise does not), unchecked `state.redirect` targets, the `'*'` CORS default, `trustProxy`, and the `X-Powered-By` header. Read it before you write an `auth` hook or deploy.
23
48
 
24
49
  ## RestServer
25
50
 
@@ -218,7 +243,7 @@ export default class MyRequest extends Request {
218
243
  ### Handler Features
219
244
 
220
245
  * Middleware support by passing an array of functions for a route
221
- * Automatic binding of `this` for instance methods
246
+ * Middleware functions are called with `this` bound to the request class; the final handler is not bound, so write it as an arrow function
222
247
  * Returns integers as HTTP status codes
223
248
  * Returns objects as JSON responses
224
249
  * Undefined responses default to `200 OK`
package/dist/main.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { type Express } from 'express';
2
2
  import type { Server } from 'http';
3
3
  export { default as Request } from './request.js';
4
+ export type { AuthHandler, RequestHandler, RequestState, RouteHandlers } from './request.js';
4
5
  export default class RestServer {
5
6
  static instance: RestServer;
6
7
  api: Express;
@@ -1 +1 @@
1
- {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAiBA,OAAgB,EAAE,KAAK,OAAO,EAAoE,MAAM,SAAS,CAAC;AAIlH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAEnC,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,cAAc,CAAC;AAElD,MAAM,CAAC,OAAO,OAAO,UAAU;IAC7B,MAAM,CAAC,QAAQ,EAAE,UAAU,CAAC;IAE5B,GAAG,EAAG,OAAO,CAAC;IACd,MAAM,EAAG,MAAM,CAAC;;IAwBhB,MAAM,CAAC,KAAK,IAAI,IAAI;IAQd,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAerB,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;IAelC,qBAAqB,IAAI,IAAI;IAW7B,UAAU,CAAC,iBAAiB,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,IAAI;CAarG"}
1
+ {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAiBA,OAAgB,EAAE,KAAK,OAAO,EAAoE,MAAM,SAAS,CAAC;AAIlH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAEnC,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,c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.1-alpha.34",
7
+ "version": "0.2.1-alpha.36",
8
8
  "description": "Rest Server Module for Stonyx Framework",
9
9
  "repository": {
10
10
  "type": "git",
@@ -34,14 +34,14 @@
34
34
  "provenance": true
35
35
  },
36
36
  "dependencies": {
37
+ "@types/express": "^5.0.6",
37
38
  "cors": "^2.8.5",
38
39
  "express": "^5.1.0",
39
- "stonyx": "0.2.3-beta.98"
40
+ "stonyx": "0.2.3-beta.100"
40
41
  },
41
42
  "devDependencies": {
42
43
  "@stonyx/utils": "0.2.3-beta.27",
43
44
  "@types/cors": "^2.8.17",
44
- "@types/express": "^5.0.6",
45
45
  "@types/node": "^25.5.2",
46
46
  "@types/qunit": "^2.19.13",
47
47
  "@types/sinon": "^21.0.1",