1 | 'use strict';
|
2 |
|
3 | const _ = require('lodash');
|
4 | const Joi = require('joi');
|
5 | const pkg = require('../package.json');
|
6 | const FormData = require('form-data');
|
7 | const FrisbySpec = require('./frisby/spec.js');
|
8 | const utils = require('./frisby/utils.js');
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const version = pkg.version;
|
15 |
|
16 | const _globalSetupDefaults = {
|
17 | request: {
|
18 | credentials: 'include',
|
19 | headers: {
|
20 | 'Content-Type': 'application/json',
|
21 | 'User-Agent': 'frisby/' + version + ' (+https://github.com/vlucas/frisby)',
|
22 | },
|
23 | rawBody: false,
|
24 | inspectOnFailure: true,
|
25 | timeout: 5000,
|
26 | },
|
27 | };
|
28 | let _globalSetup = _.cloneDeep(_globalSetupDefaults);
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | function baseUrl(url) {
|
34 | _.merge(_globalSetup, {
|
35 | request: {
|
36 | baseUrl: url,
|
37 | },
|
38 | });
|
39 | }
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | function create(name) {
|
47 | throw new Error(`
|
48 | [ERROR] frisby.create() has been removed from Frisby v2.x. If you want
|
49 | to continue using the previous latest version of Frisby 0.x, lock your
|
50 | package.json version of frisby to "0.8.5".
|
51 |
|
52 | To install and continue using v0.8.5 (unsupported):
|
53 | npm install frisby@0.8.5 --save-dev
|
54 |
|
55 | If you would like to upgrade to v2, see:
|
56 | https://github.com/vlucas/frisby/wiki/Upgrading
|
57 | `);
|
58 | }
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | function createWithAction(action, args) {
|
64 | let frisby = new module.exports.FrisbySpec();
|
65 |
|
66 |
|
67 | frisby.setup(_globalSetup);
|
68 |
|
69 |
|
70 | let params = Array.prototype.slice.call(args);
|
71 | return frisby[action].apply(frisby, params);
|
72 | }
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | function formData() {
|
78 | return new FormData();
|
79 | }
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | function globalSetup(opts) {
|
87 | _globalSetup = _.merge(_.cloneDeep(_globalSetupDefaults), opts);
|
88 | }
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | function del() {
|
94 | return createWithAction('del', arguments);
|
95 | }
|
96 | function fetch() {
|
97 | return createWithAction('fetch', arguments);
|
98 | }
|
99 | function fromJSON() {
|
100 | return createWithAction('fromJSON', arguments);
|
101 | }
|
102 | function get() {
|
103 | return createWithAction('get', arguments);
|
104 | }
|
105 | function head() {
|
106 | return createWithAction('head', arguments);
|
107 | }
|
108 | function options() {
|
109 | return createWithAction('options', arguments);
|
110 | }
|
111 | function patch() {
|
112 | return createWithAction('patch', arguments);
|
113 | }
|
114 | function post() {
|
115 | return createWithAction('post', arguments);
|
116 | }
|
117 | function put() {
|
118 | return createWithAction('put', arguments);
|
119 | }
|
120 | function setup() {
|
121 | return createWithAction('setup', arguments);
|
122 | }
|
123 | function timeout() {
|
124 | return createWithAction('timeout', arguments);
|
125 | }
|
126 | function use() {
|
127 | return createWithAction('use', arguments);
|
128 | }
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | function addExpectHandler(expectName, expectFn) {
|
134 | return module.exports.FrisbySpec.addExpectHandler(expectName, expectFn);
|
135 | }
|
136 | function removeExpectHandler(expectName, expectFn) {
|
137 | return module.exports.FrisbySpec.removeExpectHandler(expectName, expectFn);
|
138 | }
|
139 |
|
140 | module.exports = {
|
141 | addExpectHandler,
|
142 | baseUrl,
|
143 | create,
|
144 | del,
|
145 | delete: del,
|
146 | fetch,
|
147 | FrisbySpec,
|
148 | formData,
|
149 | fromJSON,
|
150 | get: get,
|
151 | globalSetup,
|
152 | head,
|
153 | Joi,
|
154 | options,
|
155 | patch,
|
156 | post,
|
157 | put,
|
158 | removeExpectHandler,
|
159 | setup,
|
160 | timeout,
|
161 | use,
|
162 | utils,
|
163 | version,
|
164 | };
|