* allow serviceData to override cacheSeconds with a longer value * prevent [endpoint] json cacheSeconds property exceeding service default * allow ShieldsRuntimeError to specify a cacheSeconds property By default error responses use the cacheLength of the service class throwing the error. This allows error to tell the handling layer the maxAge that should be set on the error badge response. * add customExceptions param This 1. allows us to specify custom properties to pass to the exception constructor if we throw any of the standard got errors e.g: `ETIMEDOUT`, `ECONNRESET`, etc 2. uses a custom `cacheSeconds` property (if set on the exception) to set the response maxAge * customExceptions --> systemErrors * errorMessages --> httpErrors
68 lines
1.5 KiB
JavaScript
68 lines
1.5 KiB
JavaScript
import { BaseJsonService } from '../index.js'
|
|
|
|
export default class GitLabBase extends BaseJsonService {
|
|
static auth = {
|
|
passKey: 'gitlab_token',
|
|
serviceKey: 'gitlab',
|
|
}
|
|
|
|
async fetch({ url, options, schema, httpErrors }) {
|
|
return this._requestJson(
|
|
this.authHelper.withBearerAuthHeader({
|
|
schema,
|
|
url,
|
|
options,
|
|
httpErrors,
|
|
})
|
|
)
|
|
}
|
|
|
|
async fetchPage({ page, requestParams, schema }) {
|
|
const { res, buffer } = await this._request(
|
|
this.authHelper.withBearerAuthHeader({
|
|
...requestParams,
|
|
...{ options: { searchParams: { page } } },
|
|
})
|
|
)
|
|
|
|
const json = this._parseJson(buffer)
|
|
const data = this.constructor._validate(json, schema)
|
|
return { res, data }
|
|
}
|
|
|
|
async fetchPaginatedArrayData({
|
|
url,
|
|
options,
|
|
schema,
|
|
httpErrors,
|
|
firstPageOnly = false,
|
|
}) {
|
|
const requestParams = {
|
|
url,
|
|
options: {
|
|
headers: { Accept: 'application/json' },
|
|
searchParams: { per_page: 100 },
|
|
...options,
|
|
},
|
|
httpErrors,
|
|
}
|
|
|
|
const {
|
|
res: { headers },
|
|
data,
|
|
} = await this.fetchPage({ page: 1, requestParams, schema })
|
|
const numberOfPages = headers['x-total-pages']
|
|
|
|
if (numberOfPages === 1 || firstPageOnly) {
|
|
return data
|
|
}
|
|
|
|
const pageData = await Promise.all(
|
|
[...Array(numberOfPages - 1).keys()].map((_, i) =>
|
|
this.fetchPage({ page: ++i + 1, requestParams, schema })
|
|
)
|
|
)
|
|
return [...data].concat(...pageData)
|
|
}
|
|
}
|