phenyl-express
Express middleware to use Phenyl in it.
// instance of PhenylRestApi (see [phenyl-rest-api](https://github.com/phenyl-js/phenyl/tree/master/modules/phenyl-rest-api).)const phenylRestApi = ...const app = appapp
Installation
npm install phenyl-express
Using types with flow
For Flow annotations, just use /jsnext
entrypoint.
API Documentation
- createPhenylApiMiddleware()
- createPhenylMiddleware()
createPhenylApiMiddleware()
Create express middleware to handle Phenyl REST APIs.
: Function // express middleware
Parameters
restApiHandler
Object, which has a method handleRequestData(reqData: RequestData): Promise<ResponseData>
.
Instance of PhenylRestApi are compatible with restApiHandler
.
pathRegex
RegExp. Optional.
Paths which match the pathRegex
are handled by Phenyl.
By default, it's /\^/api\/.*$/
; paths start with "/api/" are passed.
Example
const getVersion = async ok: 1 result: version: '1.2.3' // Settings of Phenyl REST APIconst fg = users: {} nonUsers: {} customQueries: getVersion customCommands: {} const phenylRestApi = PhenylRestApiconst app = appappapp
Client-side code will be like the following.
const client = url: 'http://localhost:3000' const result = await clientconsole // 1.2.3const text = await clientconsole // Hello, Express!
createPhenylMiddleware()
Create express middleware to handle Phenyl REST APIs and non-REST-API paths defined by customRequestHandler
.
: Function // express middleware
Why this?
It's true that Express can be easier to set your custom pages than Phenyl's customRequestHandler
.
Some non-rest entrypoints, however, are offered by Phenyl Family (like phenyl-api-explorer) and this function will be suitable for using them.
Parameters
params: ServerParams
Type of params is here:
type ServerParams = restApiHandler: RestApiHandler customRequestHandler?: Promise<EncodedHttpResponse> modifyPath?: string
restApiHandler
is the same as one described in createPhenylApiMiddleware()
.
It's an object with a method handleRequestData(reqData: RequestData): Promise<ResponseData>
.
customRequestHandler
is a function to handle general request and returns response information.
It's first argument is here:
type EncodedHttpRequest = method: 'GET' | 'POST' | 'PUT' | 'DELETE' headers: name: string: string path: string // must start with "/" qsParams?: name: string: string body?: string
The second argument restApiClient
is a client for PhenylRestApi
.
See phenyl-interfaces for the detailed interface.
It implements EntityClient
, AuthClient
and CustomClient
.
The return value must be Promise<EncodedHttpResponse>
.
type EncodedHttpResponse = +headers: name: string: string | Headers +body: string // stringified JSON or parsed JSON statusCode: number
pathRegex
RegExp. Optional.
Paths which match the pathRegex
are handled by Phenyl.
By default, it's /^/api/.$|^/explorer($|/.$)/; paths start with "/api/" or "/explorer" are passed.
/explorer
is redirected because phenyl-api-explorer use the URL.
Example
// Settings of Phenyl REST APIconst fg = users: {} nonUsers: {} customQueries: {} customCommands: {} const phenylRestApi = PhenylRestApiconst app = app appapp
Client-side code will be like the following.
const client = url: 'http://localhost:3000' const result = await clientconsole // 1.2.3const text1 = await clientconsole // Hi Phenyl, I'm John! const text2 = await clientconsole // Hello, Express!
LICENSE
Apache License 2.0