1.6.0 • Published 4 years ago

@flowio/lib-apidoc v1.6.0

Weekly downloads
88
License
MIT
Repository
github
Last release
4 years ago

lib-apidoc

Create JavaScript apidoc clients

Travis Build npm version

Code generator for apidoc that will take in an object representing an apidoc service and return a list of objects representing files for the client.

The generated code is isomorphic, depending on a native fetch implementation in the browser and node-fetch in a node.js environment.

Requirements

Installation

npm install --save-dev @flowio/lib-apidoc

Usage

A basic example of generating a JavaScipt client from an apidoc service.

import fs from 'fs';
import path from 'path';
import service from './service.json';
import { codegen } from '@flowio/lib-apidoc';

const clientBasePath = path.join(__dirname, './dist');
const client = codegen.generate(service);

client.files.forEach((file) => {
  fs.writeFileSync(path.join(clientBasePath, file.path), file.contents);
});

API Reference

codegen

Methods

The methods listed below can be accessed from codegen.

generate(service: Object, options: Object): Array

The argument passed to the generate function is:

  • service - JSON representation of a service. The service.json provided by apidoc
  • options
    • clientImportPath - path to where generated client.js file will be located relative to service resource js files. Default is . (the current directory).

Returns an array of file objects that have this shape:

{
  contents: "import Client from './client'\nexport default class Resource...",
  path: 'resource.js'
}

Generated Code

By default lib-apidoc will take in a service json object and return a list of objects representing files.

Directory Layout

If you write all of the files based on their path property you will end up with a directory structure that looks like the below.

PathDescription
./client.jsClass that handles bootstrapping a client and http (via fetch) request / response handling
./index.jsThe entrypoint to the client
./logger.jsLogging utility
./resource_1.jsA service resource that would be located at (in service.json): .resources[plural=resource_1]
./resource_2.jsA service resource that would be located at (in service.json): .resources[plural=resource_2]
./resource_3.jsA service resource that would be located at (in service.json): .resources[plural=resource_3]

Client Usage

Example creating a module that exposes a function to create an instance of a client with a default value for the api host.

import Client from './client';

export default function CreateClient(opts) {
  let options = {
    host: 'https://api.example.com',
  };

  if (typeof opts === 'string') {
    options.auth = opts;
  } else {
    options = Object.assign({}, options, opts);
  }

  return new Client(options);
}

Options

NameTypeDescription
hostStringThe http host / uri of the api
authStringConverted to a Basic Authorization
authObjectBasic or Bearer authorization
-- auth.typeStringOne of: basic, bearer, jwt. bearer and jwt are synonymous with each other
-- auth.valueStringThe value of the Authorization header
headersObjectHeaders to be passed with all http requests from the client
serviceNameStringThe name of the service (currently unused)

Resource Usage

import Client from './client'

const client = new Client({ host: 'https://api.example.com' })

// Example of using the 'get' operation on the 'users' resource.
client.users.get().then((response) => {
  switch(response.status) {
    case 200:
      response.result.map((user) =>
        console.log(`Found User: ${user.email}`))
    case 401:
      throw new Error('Was not authorized to get users!')
    default:
      throw new Error(`Response code[${response.status}] not handled!`)
  }
})

Options

All options mentioned above in the client usage and any fetch options can be provided as the only or last parameter to a resource operation.

Contributing / Issues / Questions

If you would like to contribute to lib-apidoc, have any bugs to report or have general questions, please see our contributing guidelines.

License

MIT

1.6.0

4 years ago

1.5.38

5 years ago

1.5.37

6 years ago

1.5.36

6 years ago

1.5.35

6 years ago

1.5.34

7 years ago

1.5.33

7 years ago

1.5.32

7 years ago

1.5.31

7 years ago

1.5.30

7 years ago

1.5.29

7 years ago

1.5.28

7 years ago

1.5.27

7 years ago

1.5.26

8 years ago

1.5.25

8 years ago

1.5.24

8 years ago

1.5.23

8 years ago

1.5.22

8 years ago

1.5.21

8 years ago

1.5.20

8 years ago

1.5.19

8 years ago

1.5.18

8 years ago

1.5.16-1

8 years ago

1.5.15

8 years ago

1.5.14

8 years ago

1.5.13

8 years ago

1.5.12

8 years ago

1.5.11

8 years ago

1.5.10

8 years ago

1.5.9

8 years ago

1.5.8

8 years ago

1.5.7

8 years ago

1.5.6

8 years ago

1.5.5

8 years ago

1.5.4

8 years ago

1.5.3

8 years ago

1.5.0

8 years ago

1.4.2

8 years ago

1.4.1

8 years ago

1.4.0

8 years ago

1.3.2

8 years ago

1.3.0

8 years ago

1.3.0-rc3

8 years ago

1.3.0-rc2

8 years ago

1.3.0-rc1

8 years ago

1.2.0

8 years ago

1.0.0

8 years ago

0.3.0

8 years ago

0.2.0

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.1

8 years ago