Chasten - Prithee API Reference docs
This is prithee's API reference, which is delibterately terse. The alternative is the more voluminous conceptual introduction, which has greater detail and usage guidance.
The NPM package @chasten/prithee exports the following properties.
fn
Signature
Object({ COEFFECTS, EFFECTS }) -> middleware
Description
Takes an Object with one or both of the keys COEFFECTS and EFFECTS, and returns a middleware capable of injecting coeffects into and performing side effects for the handler it wraps. The COEFFECTS option tells prithee how to calculate coeffects and the EFFECTS option tells prithee how to perform side effects. See their respective API reference entries or the general conceptual introduction for much more info.
Example
const { COEFFECTS, EFFECTS, fn } = require('@chasten/prithee');
const middleware = fn({
[COEFFECTS]: {
user(request) {
return myDatabase.statefulQuery(request.url);
},
},
[EFFECTS]: {
log(data) {
console.log(new Date(), data);
},
},
});
COEFFECTS
Signature
Type: Symbol
Usage: { [COEFFECTS]: { coeffect(request) { return data; } } }
Description
Use this symbol as key in the options objects passed to fn. The value should be an object where the keys are coeffect names and the values are functions taking a request object and returning whatever data your application needs.
Example
const { COEFFECTS, fn } = require('@chasten/prithee');
const middleware = fn({
[COEFFECTS]: {
currentTime() {
return new Date();
},
async user(request) {
const url = new URL(request.url, 'http://localhost');
const id = url.searchParams.get('userId');
return {
... await database.get(id),
... await redis.get(`rateLimit:${request.ip}`),
};
},
},
});
EFFECTS
Signature
Type: Symbol
Usage: { [EFFECTS]: { effect(yourArg) { sideEffect(); } } }
Description
Use this symbol as key in the options objects passed to fn. The value should be an object where the keys are effect names and the values are functions taking whatever you'd like as argument and carrying out the desired side effect.
Example
const { EFFECTS, fn } = require('@chasten/prithee');
const middleware = fn({
[EFFECTS]: {
log(text) {
console.log({ timestamp: new Date(), text });
},
},
});