Skip to content

CLI for creating API endpoints & schema files and converts JSON to JSON schema

Notifications You must be signed in to change notification settings

arielkwak/api-cli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Getting Started with API-CLI

This npm package creates API endpoint & schema files and converts JSON objects to JSON schemas through the command line. One can customize the directory accordingly to where the files will be created. This CLI currently will create the files in the /routes directory (in the root) and when a user inputs a specific route will create corresponding files under the directory. For instance, if the user enters '/member' for directory when prompted, the files will be created under /routes/member.

The API-CLI is built for the directory to follow the routing of the API for best practices. For more information on npm package please look at the NPM Package section or refer to the NPM Package Link.

Miscellaneous

I have worked on this project as part of an internship at Justworks (cleared with legal for uploading on my own repo), thus a lot of commits has been disregarded as forking or mirroring were prohibited.

How to run

Users can run apicli to see a little introduction of the cli. The command apicli --help will show an overview of what the cli is capable of.

Commands

There are currently two commands: apicli create and apicli json. To create an API-endpoint file, user will type in apicli create and prompted to the following prompts to create an API endpoint:

  1. User will be prompted to enter the route for the API endpoint.
  2. User will be prompted to select a RESTful method. If the selected method already exists in the given route, the CLI will exit.
  3. Depending on method (if not GET or DELETE) and if the schema files don't exist user will be asked if they would like to create the schema files (response.schema.js & body.schema.js).
  4. If the schema files exist but the user will like to create one they will have the option to do so. The new response files will be named http-method.response.schema.js, for instance, when the user selects post, the file name will be either post.response.schema.js \ post.body.schema.js. This is to prevent duplicate files and customizations could be further made on the index.js file. The import of the response schema file will be automatically setup to reflect the correct response file.

To create an JSON schema from a JSON object user will type in apicli json. The user will type in the json object they want to convert to a schema. It will able to discern objects and arrays within the given json schema. For date and date-time it will return the type to be "string" with format being either "date" or "date-time". It is currently capable of deeply nested JSON objects as well. Once the JSON schema is generated, the user can paste it to either the response.schema.js or body.schema.js file that was created from the previous steps. The JSON format currently follows the JSON to JSON Schema Converter.

Customization

The files are meant to trigger errors when first created by the CLI. Users will have to further customize the endpoint file where the @ is at which includes responseSchema, config permission, and Handler. The schema files and endpoint files for detailed coding is defaulted to be empty as the CLI will be more of providing the structure for initial building of the API endpoint.

NPM Package

This CLI is a npm package and could be installed as a dependency in any repository using the command: npm i api-json-cli Currently the API endpoint template is built to assist Fastify-CLI. Further customization on the template for API endpoint and schema files can be made on template.mjs and schemaTemplate.mjs. The routing or directory configuration can be made further as well.

Default Endpoint Template

import responseSchema from './response.schema.js';

/**  @type {import('fastify').FastifyPluginAsync} */
export default async function (fastify) {
  fastify.get('/routeName', {
    schema: {
      response: {
        200: responseSchema
      }
    },
    handler: @
  })
}


/** @type {import('fastify').RouteHandlerMethod} */
async function @Handler(request, reply) {
  throw {status: 501, message: 'Not implemented'}
}

Default Schema Template

export default { "type": "object", "properties": {} }

About

CLI for creating API endpoints & schema files and converts JSON to JSON schema

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published