Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .c8rc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"**/*.test-d.ts",
"dangerfile.js",
"core/service-test-runner",
"core/got-test-client.js",
"core/ky-test-client.js",
"services/**/*.tester.js",
"services/test-validators.js",
"services/tester.js",
Expand Down
39 changes: 20 additions & 19 deletions core/base-service/auth-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import dayjs from 'dayjs'
import Joi from 'joi'
import checkErrorResponse from './check-error-response.js'
import { InvalidParameter, InvalidResponse } from './errors.js'
import { fetch } from './got.js'
import { fetch } from './ky.js'
import { parseJson } from './json.js'
import validate from './validate.js'

Expand Down Expand Up @@ -120,8 +120,11 @@ class AuthHelper {

get _basicAuth() {
const { _user: username, _pass: password } = this
const credentials = Buffer.from(
`${username || ''}:${password || ''}`,
).toString('base64')
return this.isConfigured
? { username: username || '', password: password || '' }
? { Authorization: `Basic ${credentials}` }
: undefined
}

Expand All @@ -141,20 +144,9 @@ class AuthHelper {
return shouldAuthenticate ? mergeAuthFn(requestParams) : requestParams
}

static _mergeAuth(requestParams, auth) {
const { options, ...rest } = requestParams
return {
options: {
...auth,
...options,
},
...rest,
}
}

withBasicAuth(requestParams) {
return this._withAnyAuth(requestParams, requestParams =>
this.constructor._mergeAuth(requestParams, this._basicAuth),
this.constructor._mergeHeaders(requestParams, this._basicAuth),
)
}

Expand All @@ -175,12 +167,21 @@ class AuthHelper {
options: { headers: existingHeaders, ...restOptions } = {},
...rest
} = requestParams
const mergedHeaders =
existingHeaders instanceof Headers || Array.isArray(existingHeaders)
? Object.fromEntries(new Headers(existingHeaders))
: { ...existingHeaders }
for (const [name, value] of Object.entries(headers)) {
for (const existingName of Object.keys(mergedHeaders)) {
if (existingName.toLowerCase() === name.toLowerCase()) {
delete mergedHeaders[existingName]
}
}
mergedHeaders[name] = value
}
return {
options: {
headers: {
...existingHeaders,
...headers,
},
headers: mergedHeaders,
...restOptions,
},
...rest,
Expand Down Expand Up @@ -283,7 +284,7 @@ class AuthHelper {
const { buffer } = await checkErrorResponse({})(
await fetch(loginEndpoint, {
method: 'POST',
form: { username, password },
body: new URLSearchParams({ username, password }),
}),
)

Expand Down
46 changes: 34 additions & 12 deletions core/base-service/auth-helper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,12 @@ describe('AuthHelper', function () {
{ userKey: 'myci_user', passKey: 'myci_pass' },
{ myci_user: 'admin', myci_pass: 'abc123' },
),
]).expect({ username: 'admin', password: 'abc123' })
]).expect({ Authorization: 'Basic YWRtaW46YWJjMTIz' })
given({ userKey: 'myci_user' }, { myci_user: 'admin' }).expect({
username: 'admin',
password: '',
Authorization: 'Basic YWRtaW46',
})
given({ passKey: 'myci_pass' }, { myci_pass: 'abc123' }).expect({
username: '',
password: 'abc123',
Authorization: 'Basic OmFiYzEyMw==',
})
given({ userKey: 'myci_user', passKey: 'myci_pass' }, {}).expect(
undefined,
Expand All @@ -146,8 +144,7 @@ describe('AuthHelper', function () {
{ passKey: 'myci_pass', defaultToEmptyStringForUser: true },
{ myci_pass: 'abc123' },
).expect({
username: '',
password: 'abc123',
Authorization: 'Basic OmFiYzEyMw==',
})
})
})
Expand Down Expand Up @@ -356,8 +353,7 @@ describe('AuthHelper', function () {
}).expect({
url: 'https://myci.test/api',
options: {
username: 'admin',
password: 'abc123',
headers: { Authorization: 'Basic YWRtaW46YWJjMTIz' },
},
})
given({
Expand All @@ -368,9 +364,35 @@ describe('AuthHelper', function () {
}).expect({
url: 'https://myci.test/api',
options: {
headers: { Accept: 'application/json' },
username: 'admin',
password: 'abc123',
headers: {
Accept: 'application/json',
Authorization: 'Basic YWRtaW46YWJjMTIz',
},
},
})
given({
url: 'https://myci.test/api',
options: {
headers: { authorization: 'Bearer old-token' },
},
}).expect({
url: 'https://myci.test/api',
options: {
headers: { Authorization: 'Basic YWRtaW46YWJjMTIz' },
},
})
given({
url: 'https://myci.test/api',
options: {
headers: new Headers({ Accept: 'application/json' }),
},
}).expect({
url: 'https://myci.test/api',
options: {
headers: {
accept: 'application/json',
Authorization: 'Basic YWRtaW46YWJjMTIz',
},
},
})
})
Expand Down
15 changes: 7 additions & 8 deletions core/base-service/base-graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,17 @@ class BaseGraphqlService extends BaseService {
* representing the query clause of GraphQL POST body
* e.g. gql`{ query { ... } }`
* @param {object} attrs.variables Variables clause of GraphQL POST body
* @param {object} [attrs.options={}] Options to pass to got. See
* [documentation](https://github.com/sindresorhus/got/blob/main/documentation/2-options.md)
* @param {object} [attrs.options={}] Options to pass to Ky. See
* [documentation](https://github.com/sindresorhus/ky#options)
* @param {object} [attrs.httpErrorMessages={}] Key-value map of HTTP status codes
* and custom error messages e.g: `{ 404: 'package not found' }`.
* This can be used to extend or override the
* [default](https://github.com/badges/shields/blob/master/core/base-service/check-error-response.js#L5)
* @param {object} [attrs.systemErrors={}] Key-value map of got network exception codes
* @param {object} [attrs.systemErrors={}] Key-value map of underlying network error codes
* and an object of params to pass when we construct an Inaccessible exception object
* e.g: `{ ECONNRESET: { prettyMessage: 'connection reset' } }`.
* See {@link https://github.com/sindresorhus/got/blob/main/documentation/7-retry.md#errorcodes got error codes}
* for allowed keys
* and {@link module:core/base-service/errors~RuntimeErrorProps} for allowed values
* Codes are read from the error's cause chain. See
* {@link module:core/base-service/errors~RuntimeErrorProps} for allowed values
* @param {number[]} [attrs.logErrors=[429]] An array of http error codes
* that will be logged (to sentry, if configured).
* @param {Function} [attrs.transformJson=data => data] Function which takes the raw json and transforms it before
Expand All @@ -63,7 +62,7 @@ class BaseGraphqlService extends BaseService {
* The default is to return the first entry of the `errors` array as
* an InvalidResponse.
* @returns {object} Parsed response
* @see https://github.com/sindresorhus/got/blob/main/documentation/2-options.md
* @see https://github.com/sindresorhus/ky#options
*/
async _requestGraphql({
schema,
Expand All @@ -82,7 +81,7 @@ class BaseGraphqlService extends BaseService {
...options,
}
mergedOptions.method = 'POST'
mergedOptions.body = JSON.stringify({ query: print(query), variables })
mergedOptions.json = { query: print(query), variables }
const { buffer } = await this._request({
url,
options: mergedOptions,
Expand Down
4 changes: 2 additions & 2 deletions core/base-service/base-graphql.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ describe('BaseGraphqlService', function () {
expect(requestFetcher).to.have.been.calledOnceWith(
'http://example.com/graphql',
{
body: '{"query":"{\\n requiredString\\n}","variables":{}}',
headers: { Accept: 'application/json' },
json: { query: '{\n requiredString\n}', variables: {} },
method: 'POST',
},
)
Expand Down Expand Up @@ -80,8 +80,8 @@ describe('BaseGraphqlService', function () {
expect(requestFetcher).to.have.been.calledOnceWith(
'http://example.com/graphql',
{
body: '{"query":"{\\n requiredString\\n}","variables":{}}',
headers: { Accept: 'application/json' },
json: { query: '{\n requiredString\n}', variables: {} },
method: 'POST',
searchParams: { queryParam: 123 },
},
Expand Down
13 changes: 6 additions & 7 deletions core/base-service/base-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,21 @@ class BaseJsonService extends BaseService {
* @param {object} attrs Refer to individual attrs
* @param {Joi} attrs.schema Joi schema to validate the response against
* @param {string} attrs.url URL to request
* @param {object} [attrs.options={}] Options to pass to got. See
* [documentation](https://github.com/sindresorhus/got/blob/main/documentation/2-options.md)
* @param {object} [attrs.options={}] Options to pass to Ky. See
* [documentation](https://github.com/sindresorhus/ky#options)
* @param {object} [attrs.httpErrors={}] Key-value map of status codes
* and custom error messages e.g: `{ 404: 'package not found' }`.
* This can be used to extend or override the
* [default](https://github.com/badges/shields/blob/master/core/base-service/check-error-response.js#L5)
* @param {object} [attrs.systemErrors={}] Key-value map of got network exception codes
* @param {object} [attrs.systemErrors={}] Key-value map of underlying network error codes
* and an object of params to pass when we construct an Inaccessible exception object
* e.g: `{ ECONNRESET: { prettyMessage: 'connection reset' } }`.
* See {@link https://github.com/sindresorhus/got/blob/main/documentation/7-retry.md#errorcodes got error codes}
* for allowed keys
* and {@link module:core/base-service/errors~RuntimeErrorProps} for allowed values
* Codes are read from the error's cause chain. See
* {@link module:core/base-service/errors~RuntimeErrorProps} for allowed values
* @param {number[]} [attrs.logErrors=[429]] An array of http error codes
* that will be logged (to sentry, if configured).
* @returns {object} Parsed response
* @see https://github.com/sindresorhus/got/blob/main/documentation/2-options.md
* @see https://github.com/sindresorhus/ky#options
*/
async _requestJson({
schema,
Expand Down
13 changes: 6 additions & 7 deletions core/base-service/base-jsonl.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,23 @@ class BaseJsonlService extends BaseService {
* @param {object} attrs Refer to individual attrs
* @param {Joi} attrs.schema Joi schema to validate each response line against
* @param {string} attrs.url URL to request
* @param {object} [attrs.options={}] Options to pass to got. See
* [documentation](https://github.com/sindresorhus/got/blob/main/documentation/2-options.md)
* @param {object} [attrs.options={}] Options to pass to Ky. See
* [documentation](https://github.com/sindresorhus/ky#options)
* @param {object} [attrs.httpErrors={}] Key-value map of status codes
* and custom error messages e.g: `{ 404: 'package not found' }`.
* This can be used to extend or override the
* [default](https://github.com/badges/shields/blob/master/core/base-service/check-error-response.js#L5)
* @param {object} [attrs.systemErrors={}] Key-value map of got network exception codes
* @param {object} [attrs.systemErrors={}] Key-value map of underlying network error codes
* and an object of params to pass when we construct an Inaccessible exception object
* e.g: `{ ECONNRESET: { prettyMessage: 'connection reset' } }`.
* See {@link https://github.com/sindresorhus/got/blob/main/documentation/7-retry.md#errorcodes got error codes}
* for allowed keys
* and {@link module:core/base-service/errors~RuntimeErrorProps} for allowed values
* Codes are read from the error's cause chain. See
* {@link module:core/base-service/errors~RuntimeErrorProps} for allowed values
* @param {number[]} [attrs.logErrors=[429]] An array of http error codes
* that will be logged (to sentry, if configured).
* @param {string} [attrs.prettyErrorMessage='invalid response data']
* Error message to surface when schema validation fails.
* @returns {object[]} Parsed response
* @see https://github.com/sindresorhus/got/blob/main/documentation/2-options.md
* @see https://github.com/sindresorhus/ky#options
*/
async _requestJsonl({
schema,
Expand Down
13 changes: 6 additions & 7 deletions core/base-service/base-svg-scraping.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,21 @@ class BaseSvgScrapingService extends BaseService {
* @param {RegExp} attrs.valueMatcher
* RegExp to match the value we want to parse from the SVG
* @param {string} attrs.url URL to request
* @param {object} [attrs.options={}] Options to pass to got. See
* [documentation](https://github.com/sindresorhus/got/blob/main/documentation/2-options.md)
* @param {object} [attrs.options={}] Options to pass to Ky. See
* [documentation](https://github.com/sindresorhus/ky#options)
* @param {object} [attrs.httpErrors={}] Key-value map of status codes
* and custom error messages e.g: `{ 404: 'package not found' }`.
* This can be used to extend or override the
* [default](https://github.com/badges/shields/blob/master/core/base-service/check-error-response.js#L5)
* @param {object} [attrs.systemErrors={}] Key-value map of got network exception codes
* @param {object} [attrs.systemErrors={}] Key-value map of underlying network error codes
* and an object of params to pass when we construct an Inaccessible exception object
* e.g: `{ ECONNRESET: { prettyMessage: 'connection reset' } }`.
* See {@link https://github.com/sindresorhus/got/blob/main/documentation/7-retry.md#errorcodes got error codes}
* for allowed keys
* and {@link module:core/base-service/errors~RuntimeErrorProps} for allowed values
* Codes are read from the error's cause chain. See
* {@link module:core/base-service/errors~RuntimeErrorProps} for allowed values
* @param {number[]} [attrs.logErrors=[429]] An array of http error codes
* that will be logged (to sentry, if configured).
* @returns {object} Parsed response
* @see https://github.com/sindresorhus/got/blob/main/documentation/2-options.md
* @see https://github.com/sindresorhus/ky#options
*/
async _requestSvg({
schema,
Expand Down
13 changes: 6 additions & 7 deletions core/base-service/base-toml.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,21 @@ class BaseTomlService extends BaseService {
* @param {object} attrs Refer to individual attrs
* @param {Joi} attrs.schema Joi schema to validate the response against
* @param {string} attrs.url URL to request
* @param {object} [attrs.options={}] Options to pass to got. See
* [documentation](https://github.com/sindresorhus/got/blob/main/documentation/2-options.md)
* @param {object} [attrs.options={}] Options to pass to Ky. See
* [documentation](https://github.com/sindresorhus/ky#options)
* @param {object} [attrs.httpErrors={}] Key-value map of status codes
* and custom error messages e.g: `{ 404: 'package not found' }`.
* This can be used to extend or override the
* [default](https://github.com/badges/shields/blob/master/core/base-service/check-error-response.js#L5)
* @param {object} [attrs.systemErrors={}] Key-value map of got network exception codes
* @param {object} [attrs.systemErrors={}] Key-value map of underlying network error codes
* and an object of params to pass when we construct an Inaccessible exception object
* e.g: `{ ECONNRESET: { prettyMessage: 'connection reset' } }`.
* See {@link https://github.com/sindresorhus/got/blob/main/documentation/7-retry.md#errorcodes got error codes}
* for allowed keys
* and {@link module:core/base-service/errors~RuntimeErrorProps} for allowed values
* Codes are read from the error's cause chain. See
* {@link module:core/base-service/errors~RuntimeErrorProps} for allowed values
* @param {number[]} [attrs.logErrors=[429]] An array of http error codes
* that will be logged (to sentry, if configured).
* @returns {object} Parsed response
* @see https://github.com/sindresorhus/got/blob/main/documentation/2-options.md
* @see https://github.com/sindresorhus/ky#options
*/
async _requestToml({
schema,
Expand Down
13 changes: 6 additions & 7 deletions core/base-service/base-xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,23 @@ class BaseXmlService extends BaseService {
* @param {object} attrs Refer to individual attrs
* @param {Joi} attrs.schema Joi schema to validate the response against
* @param {string} attrs.url URL to request
* @param {object} [attrs.options={}] Options to pass to got. See
* [documentation](https://github.com/sindresorhus/got/blob/main/documentation/2-options.md)
* @param {object} [attrs.options={}] Options to pass to Ky. See
* [documentation](https://github.com/sindresorhus/ky#options)
* @param {object} [attrs.httpErrors={}] Key-value map of status codes
* and custom error messages e.g: `{ 404: 'package not found' }`.
* This can be used to extend or override the
* [default](https://github.com/badges/shields/blob/master/core/base-service/check-error-response.js#L5)
* @param {object} [attrs.systemErrors={}] Key-value map of got network exception codes
* @param {object} [attrs.systemErrors={}] Key-value map of underlying network error codes
* and an object of params to pass when we construct an Inaccessible exception object
* e.g: `{ ECONNRESET: { prettyMessage: 'connection reset' } }`.
* See {@link https://github.com/sindresorhus/got/blob/main/documentation/7-retry.md#errorcodes got error codes}
* for allowed keys
* and {@link module:core/base-service/errors~RuntimeErrorProps} for allowed values
* Codes are read from the error's cause chain. See
* {@link module:core/base-service/errors~RuntimeErrorProps} for allowed values
* @param {number[]} [attrs.logErrors=[429]] An array of http error codes
* that will be logged (to sentry, if configured).
* @param {object} [attrs.parserOptions={}] Options to pass to fast-xml-parser. See
* [documentation](https://github.com/NaturalIntelligence/fast-xml-parser#xml-to-json)
* @returns {object} Parsed response
* @see https://github.com/sindresorhus/got/blob/main/documentation/2-options.md
* @see https://github.com/sindresorhus/ky#options
* @see https://github.com/NaturalIntelligence/fast-xml-parser#xml-to-json
*/
async _requestXml({
Expand Down
Loading
Loading