npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

phenyl-express

v0.2.14

Published

Express middleware to use Phenyl in it.

Downloads

31

Readme

phenyl-expressCircleCI

Express middleware to use Phenyl in it.

import express from 'express'
import { createPhenylApiMiddleware } from 'phenyl-express'

// instance of PhenylRestApi (see [phenyl-rest-api](https://github.com/phenyl-js/phenyl/tree/master/modules/phenyl-rest-api).)
const phenylRestApi = ...
const app = express()
app.use(createPhenylApiMiddleware(phenylRestApi))
app.listen(3000)

Installation

npm install phenyl-express

Using types with flow

For Flow annotations, just use /jsnext entrypoint.

import { createPhenylApiMiddleware } from 'phenyl-express/jsnext'

API Documentation

  • createPhenylApiMiddleware()
  • createPhenylMiddleware()

createPhenylApiMiddleware()

Create express middleware to handle Phenyl REST APIs.

createPhenylApiMiddleware(
  restApiHandler: RestApiHandler,
  pathRegex: RegExp = /\^/api\/.*$/,
): 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

import express from 'express'
import { createPhenylApiMiddleware } from 'phenyl-express'
import PhenylRestApi from 'phenyl-rest-api'
import { createEntityClient } from 'phenyl-memory-db' // create DB Client used in Phenyl REST API

const getVersion = async (customQuery) => ({ ok: 1, result: { version: '1.2.3' } })

// Settings of Phenyl REST API
const fg = { users: {}, nonUsers: {}, customQueries: { getVersion }, customCommands: {} }
const phenylRestApi = PhenylRestApi.createFromFunctionalGroup(fg, { client: createEntityClient() })
const app = express()
app.use(createPhenylApiMiddleware(phenylRestApi))
app.get('/foo/bar', (req, res) => res.text(`Hello, Express!`))
app.listen(3000)

Client-side code will be like the following.

import PhenylHttpClient from 'phenyl-http-client'
const client = new PhenylHttpClient({ url: 'http://localhost:3000' })
const { result } = await client.runCustomQuery({ name: 'getVersion' })
console.log(result.version) // 1.2.3
const text = await client.requestText('/foo/bar')
console.log(text) // Hello, Express!

createPhenylMiddleware()

Create express middleware to handle Phenyl REST APIs and non-REST-API paths defined by customRequestHandler.

createPhenylMiddleware(
  params: ServerParams,
  pathRegex: RegExp = /^\/api\/.*$|^\/explorer($|\/.*$)/
): 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?: (encodedHttpRequest: EncodedHttpRequest, restApiClient: RestApiClient) => Promise<EncodedHttpResponse>,
  modifyPath?: (path: string) => 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

import express from 'express'
import { createPhenylApiMiddleware } from 'phenyl-express'
import PhenylRestApi from 'phenyl-rest-api'
import { createEntityClient } from 'phenyl-memory-db' // create DB Client used in Phenyl REST API

// Settings of Phenyl REST API
const fg = { users: {}, nonUsers: {}, customQueries: {}, customCommands: {} }
const phenylRestApi = PhenylRestApi.createFromFunctionalGroup(fg, { client: createEntityClient() })
const app = express()
app.use(createPhenylMiddleware({
  restApiHandler: phenylRestApi,
  async customRequestHandler(httpReq) {
    const { headers, body, method, qsParams, path } = httpReq
    return {
      statusCode: 200,
      headers: { 'Content-Type': 'text/plain' },
      body: `Hi Phenyl, I'm ${qsParams ? qsParams.name : 'NoName'}!`
    }
  }
}, /^\/api\/.*$|^\/abc$/))

app.get('/foo/bar', (req, res) => res.text(`Hello, Express!`))
app.listen(3000)

Client-side code will be like the following.

import PhenylHttpClient from 'phenyl-http-client'
const client = new PhenylHttpClient({ url: 'http://localhost:3000' })
const { result } = await client.runCustomQuery({ name: 'getVersion' })
console.log(result.version) // 1.2.3
const text1 = await client.requestText('/abc?name=John')
console.log(text1) // Hi Phenyl, I'm John!

const text2 = await client.requestText('/foo/bar')
console.log(text2) // Hello, Express!

LICENSE

Apache License 2.0