UNPKG

2.9 kBJavaScriptView Raw
1// API Specification
2// This document is to define server's stable API
3// - Main function
4// - options
5// - middleware
6// - context
7// - router
8// - reply
9// - utils
10
11
12
13// Main function
14const server = require('server');
15
16// Definition: an ASYNC function that accepts options and/or middleware
17server(opts, mid1, mid2, ...).then(...);
18
19// Properties (defined below)
20server.router;
21server.reply;
22server.utils;
23
24
25
26// Options
27const ops = {
28
29 // Simple options
30 port,
31 engine,
32 public,
33 secret,
34 log,
35
36 // TODO: MISSING MANY HERE; THIS PART IS NOT YET STABLE
37
38 // Plugins options
39 core,
40 parser,
41};
42
43
44
45// Middleware
46// Definition: (a)sync function, accepts the Context and returns a reply
47const mid1 = ctx => { ... };
48const mid2 = async ctx => { ... };
49
50// Return types
51// String => HTML or PLAIN response
52const mid = ctx => 'Hello world';
53// Array/Object => JSON response
54const mid = ctx => ['I', 'am', 'json'];
55const mid = ctx => ({ hello: 'world' }); // To return an object the extra () is needed
56// Reply instance
57const mid = ctx => server.reply.send('hello world');
58
59
60
61// Context
62// Definition: all of the currently known data. Varies depending on location
63// NOTE: there are more properties, but they are not considered stable
64ctx.options, // the specified or inherited options
65ctx.log, // a method to log things in several levels
66ctx.reply, // same as above
67ctx.utils, // utilities
68ctx.server, // the currently running server instance
69
70// For middleware/routers
71ctx.data, // the parsed body if it's a POST request
72ctx.params, // NOTE: NOT YET, rely on ctx.req.params so far
73ctx.query, // NOTE: NOT YET, rely on ctx.req.query so far
74// ...
75
76// Non-stable (will change at some point in the future)
77ctx.req, // express request; considering removing/changing it in 1.1
78ctx.res, // express response; not useful anymore, use server.reply instead
79
80
81
82// Router
83const router = server.router;
84const router = require('server/router');
85
86// Definition: handle the different methods and paths requested
87router.get('/', mid1);
88router.post('/users', mid2);
89router.put('/users/:id', mid3);
90
91// Methods (REST methods not explained):
92router.get;
93router.post;
94router.put;
95router.del;
96router.socket; // Handle websocket calls
97router.error; // Handle errors further up in the chain
98
99
100
101// Reply
102const reply = server.reply;
103const reply = require('server/reply');
104
105// Definition: handle the response from your code
106// Note: it MUST be returned from the middleware or it won't be executed
107reply.cookie;
108reply.download;
109reply.end;
110reply.file;
111reply.header;
112reply.json;
113reply.jsonp;
114reply.redirect;
115reply.render;
116reply.send;
117reply.status;
118reply.type;
119
120
121
122// Utils
123const utils = server.utils;
124const utils = require('server/utils'); // NOT YET AVAILABLE
125
126// Definition: some extra utilities to make development easier
127utils.modern; // Make express middleware work with server.js