feat(atlassian): implement milestone M4 - packaging and doc sync
This commit is contained in:
65
skills/atlassian/opencode/scripts/src/http.ts
Normal file
65
skills/atlassian/opencode/scripts/src/http.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { createBasicAuthHeader } from "./config.js";
|
||||
import type { AtlassianConfig, FetchLike } from "./types.js";
|
||||
|
||||
export type HttpMethod = "GET" | "POST" | "PUT";
|
||||
|
||||
export function createJsonHeaders(config: AtlassianConfig, includeJsonBody: boolean) {
|
||||
const headers: Array<[string, string]> = [
|
||||
["Accept", "application/json"],
|
||||
["Authorization", createBasicAuthHeader(config)],
|
||||
];
|
||||
|
||||
if (includeJsonBody) {
|
||||
headers.push(["Content-Type", "application/json"]);
|
||||
}
|
||||
|
||||
return headers;
|
||||
}
|
||||
|
||||
export async function parseResponse(response: Response) {
|
||||
if (response.status === 204) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const contentType = response.headers.get("content-type") ?? "";
|
||||
|
||||
if (contentType.includes("application/json")) {
|
||||
return response.json();
|
||||
}
|
||||
|
||||
return response.text();
|
||||
}
|
||||
|
||||
export async function sendJsonRequest(options: {
|
||||
config: AtlassianConfig;
|
||||
fetchImpl?: FetchLike;
|
||||
url: string;
|
||||
method: HttpMethod;
|
||||
body?: unknown;
|
||||
errorPrefix: string;
|
||||
handleResponseError?: (response: Response) => Error | undefined;
|
||||
}) {
|
||||
const fetchImpl = options.fetchImpl ?? globalThis.fetch;
|
||||
|
||||
if (!fetchImpl) {
|
||||
throw new Error("Fetch API is not available in this runtime");
|
||||
}
|
||||
|
||||
const response = await fetchImpl(options.url, {
|
||||
method: options.method,
|
||||
headers: createJsonHeaders(options.config, options.body !== undefined),
|
||||
...(options.body === undefined ? {} : { body: JSON.stringify(options.body) }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const customError = options.handleResponseError?.(response);
|
||||
|
||||
if (customError) {
|
||||
throw customError;
|
||||
}
|
||||
|
||||
throw new Error(`${options.errorPrefix}: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
return parseResponse(response);
|
||||
}
|
||||
Reference in New Issue
Block a user