@naturalcycles/js-lib 14.266.1 → 14.267.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/http/fetcher.js +10 -2
- package/dist/http/fetcher.model.d.ts +12 -0
- package/dist-esm/http/fetcher.js +10 -2
- package/package.json +1 -1
- package/src/http/fetcher.model.ts +12 -0
- package/src/http/fetcher.ts +12 -2
package/dist/http/fetcher.js
CHANGED
|
@@ -124,7 +124,10 @@ class Fetcher {
|
|
|
124
124
|
variables: opt.variables,
|
|
125
125
|
});
|
|
126
126
|
// Checking the query length, and not allowing to use GET if above 1900
|
|
127
|
-
if (opt.method === 'GET' && opt.query.length
|
|
127
|
+
if (opt.method === 'GET' && opt.query.length > 1900) {
|
|
128
|
+
opt.method = 'POST';
|
|
129
|
+
}
|
|
130
|
+
if (opt.method === 'GET') {
|
|
128
131
|
opt.searchParams = {
|
|
129
132
|
...opt.searchParams,
|
|
130
133
|
...payload,
|
|
@@ -142,6 +145,7 @@ class Fetcher {
|
|
|
142
145
|
const err = res.body.errors[0];
|
|
143
146
|
// todo: consider creating a new GraphQLError class for this
|
|
144
147
|
throw new error_util_1.HttpRequestError(err.message, {
|
|
148
|
+
...payload, // query and variables
|
|
145
149
|
errors: res.body.errors, // full errors payload returned
|
|
146
150
|
response: res.fetchResponse,
|
|
147
151
|
responseStatusCode: res.statusCode,
|
|
@@ -152,7 +156,11 @@ class Fetcher {
|
|
|
152
156
|
requestDuration: Date.now() - res.req.started,
|
|
153
157
|
});
|
|
154
158
|
}
|
|
155
|
-
|
|
159
|
+
const { data } = res.body;
|
|
160
|
+
if (opt.unwrapObject) {
|
|
161
|
+
return data[opt.unwrapObject];
|
|
162
|
+
}
|
|
163
|
+
return data;
|
|
156
164
|
}
|
|
157
165
|
// responseType=readableStream
|
|
158
166
|
/**
|
|
@@ -119,6 +119,18 @@ export interface FetcherRequest extends Omit<FetcherOptions, 'method' | 'headers
|
|
|
119
119
|
export interface FetcherGraphQLOptions extends FetcherOptions {
|
|
120
120
|
query: string;
|
|
121
121
|
variables?: AnyObject;
|
|
122
|
+
/**
|
|
123
|
+
* When querying singular entities, it may be convenient to specify 1st level object to unwrap.
|
|
124
|
+
* Example:
|
|
125
|
+
* {
|
|
126
|
+
* homePage: { ... }
|
|
127
|
+
* }
|
|
128
|
+
*
|
|
129
|
+
* unwrapObject: 'homePage'
|
|
130
|
+
*
|
|
131
|
+
* would return the contents of `{ ... }`
|
|
132
|
+
*/
|
|
133
|
+
unwrapObject?: string;
|
|
122
134
|
}
|
|
123
135
|
export interface FetcherOptions {
|
|
124
136
|
method?: HttpMethod;
|
package/dist-esm/http/fetcher.js
CHANGED
|
@@ -117,7 +117,10 @@ export class Fetcher {
|
|
|
117
117
|
variables: opt.variables,
|
|
118
118
|
});
|
|
119
119
|
// Checking the query length, and not allowing to use GET if above 1900
|
|
120
|
-
if (opt.method === 'GET' && opt.query.length
|
|
120
|
+
if (opt.method === 'GET' && opt.query.length > 1900) {
|
|
121
|
+
opt.method = 'POST';
|
|
122
|
+
}
|
|
123
|
+
if (opt.method === 'GET') {
|
|
121
124
|
opt.searchParams = {
|
|
122
125
|
...opt.searchParams,
|
|
123
126
|
...payload,
|
|
@@ -135,6 +138,7 @@ export class Fetcher {
|
|
|
135
138
|
const err = res.body.errors[0];
|
|
136
139
|
// todo: consider creating a new GraphQLError class for this
|
|
137
140
|
throw new HttpRequestError(err.message, {
|
|
141
|
+
...payload, // query and variables
|
|
138
142
|
errors: res.body.errors, // full errors payload returned
|
|
139
143
|
response: res.fetchResponse,
|
|
140
144
|
responseStatusCode: res.statusCode,
|
|
@@ -145,7 +149,11 @@ export class Fetcher {
|
|
|
145
149
|
requestDuration: Date.now() - res.req.started,
|
|
146
150
|
});
|
|
147
151
|
}
|
|
148
|
-
|
|
152
|
+
const { data } = res.body;
|
|
153
|
+
if (opt.unwrapObject) {
|
|
154
|
+
return data[opt.unwrapObject];
|
|
155
|
+
}
|
|
156
|
+
return data;
|
|
149
157
|
}
|
|
150
158
|
// responseType=readableStream
|
|
151
159
|
/**
|
package/package.json
CHANGED
|
@@ -155,6 +155,18 @@ export interface FetcherRequest
|
|
|
155
155
|
export interface FetcherGraphQLOptions extends FetcherOptions {
|
|
156
156
|
query: string
|
|
157
157
|
variables?: AnyObject
|
|
158
|
+
/**
|
|
159
|
+
* When querying singular entities, it may be convenient to specify 1st level object to unwrap.
|
|
160
|
+
* Example:
|
|
161
|
+
* {
|
|
162
|
+
* homePage: { ... }
|
|
163
|
+
* }
|
|
164
|
+
*
|
|
165
|
+
* unwrapObject: 'homePage'
|
|
166
|
+
*
|
|
167
|
+
* would return the contents of `{ ... }`
|
|
168
|
+
*/
|
|
169
|
+
unwrapObject?: string
|
|
158
170
|
}
|
|
159
171
|
|
|
160
172
|
export interface FetcherOptions {
|
package/src/http/fetcher.ts
CHANGED
|
@@ -194,7 +194,11 @@ export class Fetcher {
|
|
|
194
194
|
})
|
|
195
195
|
|
|
196
196
|
// Checking the query length, and not allowing to use GET if above 1900
|
|
197
|
-
if (opt.method === 'GET' && opt.query.length
|
|
197
|
+
if (opt.method === 'GET' && opt.query.length > 1900) {
|
|
198
|
+
opt.method = 'POST'
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (opt.method === 'GET') {
|
|
198
202
|
opt.searchParams = {
|
|
199
203
|
...opt.searchParams,
|
|
200
204
|
...payload,
|
|
@@ -214,6 +218,7 @@ export class Fetcher {
|
|
|
214
218
|
|
|
215
219
|
// todo: consider creating a new GraphQLError class for this
|
|
216
220
|
throw new HttpRequestError(err.message, {
|
|
221
|
+
...payload, // query and variables
|
|
217
222
|
errors: res.body.errors, // full errors payload returned
|
|
218
223
|
response: res.fetchResponse,
|
|
219
224
|
responseStatusCode: res.statusCode,
|
|
@@ -225,7 +230,12 @@ export class Fetcher {
|
|
|
225
230
|
})
|
|
226
231
|
}
|
|
227
232
|
|
|
228
|
-
|
|
233
|
+
const { data } = res.body
|
|
234
|
+
if (opt.unwrapObject) {
|
|
235
|
+
return (data as any)[opt.unwrapObject]
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return data
|
|
229
239
|
}
|
|
230
240
|
|
|
231
241
|
// responseType=readableStream
|