Skip to content

Latest commit

 

History

History
79 lines (67 loc) · 2.53 KB

File metadata and controls

79 lines (67 loc) · 2.53 KB

pcolib-logo

API

Run a request

The argument example of method Pcolib.prototype.run() is optional. If omitted, the data present in original request is used.

import collection from './postman.json' assert {type: 'json'}
import Pcolib from 'postman-collection-interface'

const pcolib = new Pcolib({ collection })
pcolib.run('folder-name', 'request-name', 'example-name')

Specify by method-chaining

pcolib.get('folder', 'req').example('ex').run()

pcolib.get('folder').request('req').example('ex').run()

Specify request data

pcolib.get('public-api', 'user login').body({ name: 'muhammad' }).run()

pcolib.get('public-api')
	.request('user login')
	.body({ name: 'muhammad' })
	.headers({ etag: 'etag-token' })
	.run()

Get request data

const {body, query, headers, params} = pcolib.get('public-api', 'user login', 'dev: ok')

URL

Pcolib parses and looks out for query parameters and path variables. If these data are specified by user, then they're used to make the request. If a URL in postman is as: /user/{{user_id}}/repositories/{{repo_id}}, then:

pcolib.get('user code repo')
	.request('get single repo')
	.headers({ authorization: 'Bearer xyz' })
	.params({ user_id: 'f7kla9-09kda', repo_id: '345678' })

Stateful requests

Pcolib instances can be a mock for a user. Often useful in end-to-end testing. If global payload is specified, all requests from the instance will contain the payload.

const admin = new Pcolib({
	collection,
	global: { headers: { authorization: 'Bearer admin' } }
})
const shop = new Pcolib({
	collection,
	global: { headers: { authorization: 'Bearer shop' } }
})
const user = new Pcolib({
	collection,
	global: { headers: { authorization: 'Bearer user' } }
})

user.get('cart', 'checkout').run()
shop.get('order', 'list').run()
admin.get('analytics', 'past orders').query({ pastDays: 30 }).run()