Browse Source

fix onHttpErrors resolve behaviour

tags/v1.0.0
ojizero 1 year ago
parent
commit
44e599c046
No account linked to committer's email address
5 changed files with 45 additions and 39 deletions
  1. 1
    1
      README.md
  2. 29
    25
      examples/sample-elasticsearch-client/index.js
  3. 1
    1
      package-lock.json
  4. 1
    1
      package.json
  5. 13
    11
      src/client.ts

+ 1
- 1
README.md View File

@@ -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)

+ 29
- 25
examples/sample-elasticsearch-client/index.js View File

@@ -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) {
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) {
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) } })
})()
}

+ 1
- 1
package-lock.json View File

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

+ 1
- 1
package.json View File

@@ -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": {

+ 13
- 11
src/client.ts View File

@@ -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 {
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 {

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 {
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 {
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