From 849b823311e299fd23312e23fecf233996c18e9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20D=C3=ADaz?= Date: Sat, 21 Nov 2020 21:59:03 -0600 Subject: [PATCH] Update example (#163) * Moving from inquirer to prompts. It has a smaller dependency graph and we don't need the fancy features inquirer has. * Added a single better example to keep updated as I finalize the schema. --- examples/beau.yml | 23 ++++++++++++++++++++++ examples/github.yml | 15 --------------- examples/hosts.yml | 22 --------------------- examples/httpbin.yml | 27 -------------------------- examples/jsonplaceholder.yml | 29 ---------------------------- examples/plugins.yml | 11 ----------- examples/slack.yml | 37 ------------------------------------ schema.json | 15 ++++++++++++++- src/request.js | 7 ++++--- 9 files changed, 41 insertions(+), 145 deletions(-) create mode 100644 examples/beau.yml delete mode 100644 examples/github.yml delete mode 100644 examples/hosts.yml delete mode 100644 examples/httpbin.yml delete mode 100644 examples/jsonplaceholder.yml delete mode 100644 examples/plugins.yml delete mode 100644 examples/slack.yml diff --git a/examples/beau.yml b/examples/beau.yml new file mode 100644 index 0000000..e18acb0 --- /dev/null +++ b/examples/beau.yml @@ -0,0 +1,23 @@ +endpoint: https://pokeapi.co/api/v2 + +# Try replacing this pokemon using params: +# beau request get-pokemon -P "pokemon=dragapult" +environment: + _: + pokemon: ditto + +GET /pokemon/$env._.pokemon: get-pokemon +GET $get-pokemon.body.location_area_encounters: get-encounters + +--- +host: httpbin +endpoint: https://httpbin.org + +POST /anything: + - alias: post-first-area + payload: + area: $get-encounters.body.0.location_area.name + + - alias: post-pokemon-type + payload: + type: $get-pokemon.body.types.0.type.name diff --git a/examples/github.yml b/examples/github.yml deleted file mode 100644 index 4b6dd0c..0000000 --- a/examples/github.yml +++ /dev/null @@ -1,15 +0,0 @@ -VERSION: 1 -ENDPOINT: https://api.github.com - -auth: &auth - HEADERS: - Authorization: token asfdasf123423sd1fgnh7d83n478 - User-Agent: Beau - -GET /user: - ALIAS: $user - <<: *auth - -GET /user/repos: - ALIAS: $repos - <<: *auth diff --git a/examples/hosts.yml b/examples/hosts.yml deleted file mode 100644 index ea8ea08..0000000 --- a/examples/hosts.yml +++ /dev/null @@ -1,22 +0,0 @@ -version: 1 -endpoint: http://httpbin.org - -environment: - the: - post: 2 - -defaults: - headers: - hello: 'Hello2' - -POST /anything: - alias: anything - payload: - title: $jpa:get-post.body.title - ---- -host: jpa -endpoint: http://jsonplaceholder.typicode.com - -GET /posts/$env.the.post: get-post -GET /users/$jpa:get-post.body.userId: hello diff --git a/examples/httpbin.yml b/examples/httpbin.yml deleted file mode 100644 index 3210d24..0000000 --- a/examples/httpbin.yml +++ /dev/null @@ -1,27 +0,0 @@ -version: 1 -endpoint: https://httpbin.org/ - -cookiejar: true -environment: - params: - name: David - -GET /anything: - - alias: anything - payload: - name: $env.params.name - - alias: anything2 - -GET /cookies/set: - alias: set-cookies - params: - hello: World - -GET /status/418: - alias: teapot - -POST /post: - alias: post - formdata: - id: $[uuid()] - file: $[createReadStream('./github.yml')] diff --git a/examples/jsonplaceholder.yml b/examples/jsonplaceholder.yml deleted file mode 100644 index 8f703f7..0000000 --- a/examples/jsonplaceholder.yml +++ /dev/null @@ -1,29 +0,0 @@ -version: 1 -endpoint: 'http://jsonplaceholder.typicode.com' - -environment: - the: - post: 1 - -defaults: - headers: - hello: $posts.body.0.userId - -GET /posts/$env.the.post: get-post - -GET /posts/: - alias: posts - headers: - hello: false - -POST /posts/: - alias: new-post - documentation: - title: New Post - -GET /users/$posts.body.0.userId: - alias: post-user - documentation: - description: Fetches the user for a give post. - params: - hello: 'world' diff --git a/examples/plugins.yml b/examples/plugins.yml deleted file mode 100644 index 12718e2..0000000 --- a/examples/plugins.yml +++ /dev/null @@ -1,11 +0,0 @@ -endpoint: http://localhost:10080 - -plugins: - - jwt: - data: - userId: 12 - name: Sergio - secret: 'asdfasdf+asdfasdf/asdfasdfasdfasdf==' - -GET /test: - alias: test diff --git a/examples/slack.yml b/examples/slack.yml deleted file mode 100644 index 30ab8ec..0000000 --- a/examples/slack.yml +++ /dev/null @@ -1,37 +0,0 @@ -VERSION: '1' -ENDPOINT: https://slack.com/api - -auth: &auth - token: xoxp-139455775026-139455775090-147751461120-f224ed6ffee029869a0f138d0859e7d6 - -GET /users.getPresence: - ALIAS: presence - PARAMS: - <<: *auth - -GET /channels.list: - ALIAS: channel-list - PARAMS: - <<: *auth - exclude_archived: true - -GET /channels.info: - ALIAS: channel-info - PARAMS: - <<: *auth - channel: $channel-list.response.body.channels.0.id - -POST /chat.postMessage: - ALIAS: new-message - PARAMS: - <<: *auth - channel: $channel-info.response.body.channel.id - text: 'Hey Seich!' - parse: full - link_names: true - username: Beau - -GET /users.list: - ALIAS: user-list - PARAMS: - <<: *auth diff --git a/schema.json b/schema.json index c3b3d3d..73c0fc4 100644 --- a/schema.json +++ b/schema.json @@ -71,6 +71,9 @@ "cookiejar": { "type": "boolean" }, + "host": { + "type": "string" + }, "defaults": { "$ref": "#/definitions/requestObject" }, @@ -83,7 +86,17 @@ ] } }, - "environment": {} + "environment": { + "type": "object", + "additionalProperties": true, + "properties": { + "_": { + "type": "object", + "description": "Environment variables brought in by cli params or dotenv.", + "additionalProperties": true + } + } + } }, "patternProperties": { "(GET|HEAD|POST|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH)\\s.*": { diff --git a/src/request.js b/src/request.js index 9c05fc4..0221394 100644 --- a/src/request.js +++ b/src/request.js @@ -64,10 +64,8 @@ class Request { } async exec(cache = new RequestCache()) { - const isPathFullUrl = isUrl(this.PATH) - let settings = cache.parse({ - baseUrl: isPathFullUrl ? '' : this.ENDPOINT, + baseUrl: '', uri: this.PATH, method: this.VERB, jar: this.COOKIEJAR, @@ -83,6 +81,9 @@ class Request { resolveWithFullResponse: true }) + const isPathFullUrl = isUrl(settings.uri) + settings.baseUrl = isPathFullUrl ? '' : this.ENDPOINT + settings = removeOptionalKeys(settings, [ 'headers', 'qs',