0.0.21 • Published 2 years ago

@karrotframe/pathfinder v0.0.21

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 years ago

@karrotframe/pathfinder

code generator tool for routes by schema

Table of Contents

Installation

$ yarn add @karrotframe/pathfinder

Usage

$ yarn pathfinder init # create initial files
$ yarn pathfinder generate -s schema.json # generate code file from schema

CLI Option

  • -s, --source : load json file defining schema
$ yarn pathfinder generate --source schema.json # load local json file
$ yarn pathfinder generate --source https://example.com/example.json # load remote json file

  • -o, --output : Specify directory path to generate result file ( default: __generated__ )
$ yarn pathfinder generate -s schema.json  --output ./result

  • -r, --replace : Replace generator function with specific module from npm package
$ yarn pathfinder generate -s schema.json -r custom-generator-name

  • -u, --suffix : name to describe result type. First letter will be capitalized automatically ( default: Sdk )
$ yarn pathfinder generate -s schema.json -u result

  • -d, --debug : Turn on debug mode to display all messages
$ yarn pathfinder generate -d

.pathfinderrc config file

A config file could be used instead of CLI option. Note that option of config file would be ignored when any same option have been declared from CLI option and from config file both.

  • source : path for json schema file.
  • output : path for generated result.
  • replace : custom generator function with specific module from npm package to replace a basic built-in function.
  • suffix : name to describe result type.

Entire Schema Definition

nametypedescriptionexample
nameStringname to generate"example"
descriptionStringDescription for schema"sdk for example route"
authorStringAuthor who is responsible for schema"John Doe"
endpointStringDeclare domain URL"https://example.com"
endpointsObjectKey is environment and value is domain URL. If endpoint is also declared, endpoint should be ignored.{ prod: "https://example.com"}
versionNumberVersion for generated result1
routesArrayArray contains Route elements.

Route Schema Definition

nametypedescriptionexample
nameStringname for route page"guitar"
descriptionStringdescription for route method"Method to open guitar detail page"
pathStringURI for route page. If you declare param like :exampleId, this param will be parsed into first parameter of method i.e. : item/:id/comments/:subId -> foo({ id, subId })"/product/guitar/:guitarId"
queryParamsJSON SchemaqueryParams will be parsed second parameter of method and properties should be usable as query string. Although you do not need queryParams, you should declare it with { "additionalProperties": false } currently. It should be fixed soon.please, see below

queryParams example

{
  "type": "object",
  "properties": {
    "referrer": {
      "type": "string",
      "description": "tracking referrer page",
      "enum": ["guitar"]
    }
  },
  "required": ["referrer"],
  "additionalProperties": false
}

Entire Schema Example

{
  "name": "example",
  "description": "sdk for example route",
  "author": "John Doe",
  "endpoint": "https://example.com",
  "endpoints": {
    "prod": "https://example.prod.com"
  },
  "version": 1,
  "routes": [
    {
      "name": "guitar",
      "path": "/product/guitar/:guitarId",
      "description": "Method to open guitar detail page",
      "queryParams": {
        "additionalProperties": false
      }
    },
    {
      "name": "accessory",
      "path": "/product/accessory/:accessoryId",
      "description": "Method to open accessory detail page",
      "queryParams": {
        "type": "object",
        "properties": {
          "referrer": {
            "type": "string",
            "description": "tracking referrer page",
            "enum": ["guitar"]
          }
        },
        "required": ["referrer"],
        "additionalProperties": false
      }
    }
  ]
}

Redefine onOpen callback for usage

To customize methods, you should redefine onOpen callback.

import customRoute from 'customRoute'
import { makeExampleSdk } from '__generated__'

const { openRouteJobs } = makeExampleSdk({
  onOpen: (endpoint: string, path: string) => {
    const targetPath = endpoint + path
    this.bridge.router.push(targetPath)
  },
})

const handler = () => {
  const params = { newguitarId: 'constant' }
  openExampleGuitar(params)
}
0.0.20

2 years ago

0.0.21

2 years ago

0.0.19

2 years ago

0.0.12

2 years ago

0.0.13

2 years ago

0.0.14

2 years ago

0.0.15

2 years ago

0.0.9

2 years ago

0.0.16

2 years ago

0.0.8

2 years ago

0.0.17

2 years ago

0.0.18

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago