mirror of
https://github.com/jdalrymple/gitbeaker.git
synced 2026-01-18 15:55:30 +00:00
Gitbeaker has been split up into 5 subpackages: gitbeaker-core, gitbeaker-node, gitbeaker-cli, gitbeaker-browser and gitbeaker-requester-utils.
gitbeaker-[node,cli,browser] are enviroment
specific sub packages. For example, if you want to use gitbeaker in a NodeJS environment, use gitbeaker-node. gitbeaker-core is where all the
base logic exists, and gitbeaker-requester-utils is a collection of utility functions for making custom requester libraries.
BREAKING CHANGE: 🧨 This migration requires users to import specific subpackages. For NodeJS
usage, that would be @gitbeaker/node.
66 lines
1.5 KiB
TypeScript
66 lines
1.5 KiB
TypeScript
import Got from 'got';
|
|
import FormData from 'form-data';
|
|
import { decamelizeKeys } from 'xcase';
|
|
import {
|
|
Service,
|
|
DefaultRequestOptions,
|
|
createInstance,
|
|
defaultRequest as baseDefaultRequest,
|
|
} from '@gitbeaker/requester-utils';
|
|
|
|
export function defaultRequest(
|
|
service: Service,
|
|
{ body, query, sudo, method }: DefaultRequestOptions = {},
|
|
) {
|
|
const options = baseDefaultRequest(service, { body, query, sudo, method });
|
|
|
|
if (typeof body === 'object' && !(body instanceof FormData)) {
|
|
options.json = decamelizeKeys(body);
|
|
|
|
delete options.body;
|
|
}
|
|
|
|
return options;
|
|
}
|
|
|
|
export function processBody(response) {
|
|
const contentType = response.headers['content-type'] || '';
|
|
|
|
switch (contentType) {
|
|
case 'application/json': {
|
|
return response.body.length === 0 ? {} : JSON.parse(response.body.toString());
|
|
}
|
|
case 'application/octet-stream':
|
|
case 'binary/octet-stream':
|
|
case 'application/gzip': {
|
|
return Buffer.from(response.body);
|
|
}
|
|
default: {
|
|
return response.body || '';
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function handler(endpoint, options) {
|
|
let response;
|
|
|
|
try {
|
|
response = await Got(endpoint, options);
|
|
} catch (e) {
|
|
if (e.response) {
|
|
const output = e.response.body;
|
|
|
|
e.description = output.error || output.message;
|
|
}
|
|
|
|
throw e;
|
|
}
|
|
|
|
const { statusCode, headers } = response;
|
|
const body = processBody(response);
|
|
|
|
return { body, headers, status: statusCode };
|
|
}
|
|
|
|
export const Requester = createInstance(defaultRequest, handler);
|