|
65 | 65 | } |
66 | 66 | ``` |
67 | 67 |
|
| 68 | +This module ships with a handwritten TypeScript declaration file for TS support. The declaration exports a single namespace `LightMyRequest`. You can import it one of two ways: |
| 69 | +```typescript |
| 70 | +import * as LightMyRequest from 'light-my-request' |
| 71 | + |
| 72 | +const dispatch: LightMyRequest.DispatchFunc = function (req, res) { |
| 73 | + const reply = 'Hello World' |
| 74 | + res.writeHead(200, { 'Content-Type': 'text/plain', 'Content-Length': reply.length }) |
| 75 | + res.end(reply) |
| 76 | +} |
| 77 | + |
| 78 | +LightMyRequest.inject(dispatch, { method: 'get', url: '/' }, (err, res) => { |
| 79 | + console.log(res.payload) |
| 80 | +}) |
| 81 | + |
| 82 | +// or |
| 83 | +import { inject, DistpatchFunc } from 'light-my-request' |
| 84 | + |
| 85 | +const dispatch: DispatchFunc = function (req, res) { |
| 86 | + const reply = 'Hello World' |
| 87 | + res.writeHead(200, { 'Content-Type': 'text/plain', 'Content-Length': reply.length }) |
| 88 | + res.end(reply) |
| 89 | +} |
| 90 | + |
| 91 | +inject(dispatch, { method: 'get', url: '/' }, (err, res) => { |
| 92 | + console.log(res.payload) |
| 93 | +}) |
| 94 | +``` |
| 95 | +The declaration file exports types for the following parts of the API: |
| 96 | +- `inject` - standard light-my-request `inject` method |
| 97 | +- `DispatchFunc` - the fake HTTP dispatch function |
| 98 | +- `InjectPayload` - a union type for valid payload types |
| 99 | +- `isInjection` - standard light-my-request `isInjection` method |
| 100 | +- `InjectOptions` - options object for `inject` method |
| 101 | +- `Request` - custom light-my-request `request` object interface. Extends Node.js `stream.Readable` type |
| 102 | +- `Response` - custom light-my-re uest `response` object interface. Extends Node.js `http.ServerResponse` type |
| 103 | + |
68 | 104 | ## API |
69 | 105 |
|
70 | 106 | #### `inject(dispatchFunc, options, callback)` |
|
0 commit comments