nuxt-kql
Version:
Kirby's Query Language API for Nuxt
33 lines (32 loc) • 900 B
JavaScript
import { useRuntimeConfig } from "#imports";
import { joinURL } from "ufo";
import { createAuthHeader, headersToObject } from "../utils.js";
export function $kirby(path, opts = {}) {
const { headers, language, ...fetchOptions } = opts;
const kql = useRuntimeConfig().kql;
if (language)
path = joinURL(language, path);
return globalThis.$fetch(path, {
...fetchOptions,
baseURL: kql.url,
headers: {
...headersToObject(headers),
...createAuthHeader(kql)
}
});
}
export function $kql(query, opts = {}) {
const { headers, language, ...fetchOptions } = opts;
const kql = useRuntimeConfig().kql;
return globalThis.$fetch(kql.prefix, {
...fetchOptions,
baseURL: kql.url,
method: "POST",
body: query,
headers: {
...headersToObject(headers),
...createAuthHeader(kql),
...language && { "X-Language": language }
}
});
}