Browse Source

fix onHttpErrors resolve behaviour

modified-method-api
ojizero 4 years ago
parent
commit
44e599c046
No known key found for this signature in database
GPG Key ID: FEBB7097845B0C7E
  1. 2
      README.md
  2. 54
      examples/sample-elasticsearch-client/index.js
  3. 2
      package-lock.json
  4. 2
      package.json
  5. 24
      src/client.ts

2
README.md

@ -347,7 +347,7 @@ This is still a work in progress :D any help is appreciated @@ -347,7 +347,7 @@ This is still a work in progress :D any help is appreciated
- [x] Why?
- [x] More on external API
- [ ] Support non JSON payload
- [ ] Get `onError: resolve` to work
- [x] Get `onError: resolve` to work
- [x] Support simplified form for validation
- [x] Finalize behvaiour of genreator method functions
- [x] What to do with their arguments (ambiguity of options/payload/querystring in args)

54
examples/sample-elasticsearch-client/index.js

@ -6,7 +6,7 @@ const { @@ -6,7 +6,7 @@ const {
function makeESClient (host) {
const client = createPortalClient({
baseUrl: host,
onError: 'resolve',
onHttpErrors: 'resolve',
})
return {
@ -21,31 +21,31 @@ function makeESClient (host) { @@ -21,31 +21,31 @@ function makeESClient (host) {
addDocument: client.route({
path: '/:indexName/:docType/:docId',
method: 'POST',
})
}),
// TODO: elastic's bulk API currently is incompatible with portal :D
// bulkOperation: client.route({
// path: '/_bulk',
// method: 'POST',
// // You can pass any Joi based schema for validation
// // Or you can use the simplified syntax provided by portal
// // Or you can use any custom object withe a `validate` method
// body: Joi.string().required(),
// // Joi.object({
// // body: Joi.array().items(
// // Joi.object({
// // index: Joi.object({
// // _index: Joi.string().required(),
// // _type: Joi.string().required(),
// // _id: Joi.string(),
// // }).required(),
// // }),
// // Joi.object(),
// // ).required()
// // }).required(),
// // headers: {
// // 'Content-Type': 'application/text',
// // }
// }),
bulkOperation: client.route({
path: '/_bulk',
method: 'POST',
// You can pass any Joi based schema for validation
// Or you can use the simplified syntax provided by portal
// Or you can use any custom object withe a `validate` method
// body: Joi.string().required(),
// Joi.object({
// body: Joi.array().items(
// Joi.object({
// index: Joi.object({
// _index: Joi.string().required(),
// _type: Joi.string().required(),
// _id: Joi.string(),
// }).required(),
// }),
// Joi.object(),
// ).required()
// }).required(),
// headers: {
// 'Content-Type': 'application/text',
// }
}),
}
}
@ -67,5 +67,9 @@ if (require.main === module) { @@ -67,5 +67,9 @@ if (require.main === module) {
response = await client.getDocument('test-index', 'test-type', 'test-id')
delete response._rawResponse // This is a HUGE object
console.log({ getDocument: { stringifiedResponse: JSON.stringify(response) } })
response = await client.bulkOperation()
delete response._rawResponse // This is a HUGE object
console.log({ getDocument: { stringifiedResponse: JSON.stringify(response) } })
})()
}

2
package-lock.json generated

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
{
"name": "@ojizero/portal",
"version": "1.0.0-alpha.3",
"version": "1.0.0-alpha.4",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

2
package.json

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
{
"name": "@ojizero/portal",
"version": "1.0.0-alpha.3",
"version": "1.0.0-alpha.4",
"description": "HTTP API clients ... simplified.",
"main": "lib/index.js",
"publishConfig": {

24
src/client.ts

@ -82,7 +82,7 @@ export interface Config { @@ -82,7 +82,7 @@ export interface Config {
authentication?: Authentication,
retries?: number, // available from RequestOptions
timeout?: Seconds, // available from HttpsRequestOptions
onError?: 'reject' | 'resolve',
onHttpErrors?: 'reject' | 'resolve',
}
export type ClientFn = (options: RequestOptions) => Promise<RawResponse>
@ -97,6 +97,7 @@ export interface Client { @@ -97,6 +97,7 @@ export interface Client {
export class PortalClient implements Client {
config: Config
client: ClientFn
defaultRequestOptions: RequestConfig
constructor (client: ClientFn, config: Config) {
// TODO: this shouldn't exists but i had to set baseUrl as otpional to construc the RequestConfig type
@ -104,6 +105,14 @@ export class PortalClient implements Client { @@ -104,6 +105,14 @@ export class PortalClient implements Client {
this.client = client
this.config = config
this.defaultRequestOptions = {
// port: 443,
retries: 0,
headers: {},
timeout: 30,
onHttpErrors: 'reject',
// protocol: 'https',
}
}
async request (method: string, path: string, payload: RequestBody, options: RequestConfig): Promise<Response> {
@ -120,16 +129,9 @@ export class PortalClient implements Client { @@ -120,16 +129,9 @@ export class PortalClient implements Client {
headers,
retries,
timeout,
onError,
onHttpErrors,
authentication,
} = defaultsDeep({
// port: 443,
retries: 0,
headers: {},
timeout: 30,
onError: 'reject',
// protocol: 'https',
}, this.config, options)
} = defaultsDeep({}, options, this.config, this.defaultRequestOptions)
if (typeof authentication !== 'undefined') {
const {
@ -164,7 +166,7 @@ export class PortalClient implements Client { @@ -164,7 +166,7 @@ export class PortalClient implements Client {
retries,
json: isJson,
timeout: timeout * 1000,
throwHttpErrors: onError !== 'resolve',
throwHttpErrors: onHttpErrors !== 'resolve',
// TODO: what if the payload is undefined ?
body: isJson ? payload : JSON.stringify(payload),
}

Loading…
Cancel
Save