55 <img src="https://img.shields.io/npm/v/@saborter/server?color=red&label=npm%20package" /></a>
66<a href =" https://www.npmjs.com/package/@saborter/server " alt =" Npm downloads " >
77 <img src="https://img.shields.io/npm/dm/@saborter/server.svg" /></a>
8+ <a href =" https://github.com/TENSIILE/saborter-server/actions/workflows/publish.yml " alt =" Release " >
9+ <img src="https://github.com/TENSIILE/saborter-server/actions/workflows/publish.yml/badge.svg" /></a>
810<a href =" https://github.com/TENSIILE/saborter-server/actions/workflows/ci.yml " alt =" CI " >
911 <img src="https://github.com/TENSIILE/saborter-server/actions/workflows/ci.yml/badge.svg" /></a>
1012<a href =" https://github.com/TENSIILE/saborter-server/blob/develop/LICENSE " alt =" License " >
2022The documentation is divided into several sections:
2123
2224- [ Installation] ( #📦-installation )
25+ - [ Quick Start] ( #🚀-quick-start )
26+ - [ Typescript] ( #🔖-typescript )
2327- [ License] ( #📋-license )
2428
2529## 📦 Installation
@@ -30,6 +34,60 @@ npm install saborter @saborter/server
3034yarn add saborter @saborter/server
3135```
3236
37+ ## 🚀 Quick Start
38+
39+ ### Basic Usage for Express
40+
41+ ``` typescript
42+ import express from ' express' ;
43+ import { isAbortError } from ' saborter/lib' ;
44+ import { initRequestInterrupts , getAbortSignal } from ' @saborter/server/express' ;
45+
46+ const app = express ();
47+ const port = process .env .PORT || 3000 ;
48+
49+ initRequestInterrupts (app , { endpointName: ' /api/abort' });
50+ app .use (express .json ());
51+
52+ app .get (' /' , async (req , res ) => {
53+ try {
54+ const result = await longRunningOperation (getAbortSignal (req ));
55+ res .json (result );
56+ } catch (error ) {
57+ if (isAbortError (error )) {
58+ return res .status (499 ).send ();
59+ }
60+
61+ res .status (500 ).send ();
62+ }
63+ });
64+
65+ app .listen (port , () => {
66+ console .log (` Server running at http://localhost:${port } ` );
67+ });
68+ ```
69+
70+ ## 🔖 Typescript
71+
72+ ### Signal Typing in Express Request
73+
74+ If you don't want to use the ` getAbortSignal ` function, you can declare the ` Request ` interface:
75+
76+ ``` typescript
77+ declare namespace Express {
78+ interface Request {
79+ signal? : AbortSignal ;
80+ }
81+ }
82+ ```
83+
84+ After the declaration, you can directly retrieve the signal from the request:
85+
86+ ``` typescript
87+ const result = await longRunningOperation (req .signal );
88+ res .json (result );
89+ ```
90+
3391## 📋 License
3492
3593MIT License - see [ LICENSE] ( ./LICENSE ) for details.
0 commit comments