UNPKG

@phalcode/ts-igdb-client

Version:

A Typescript client and request builder for IGDB using ts-apicalypse

216 lines (212 loc) 6.32 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { WhereFlags: () => import_ts_apicalypse2.WhereFlags, WhereInFlags: () => import_ts_apicalypse2.WhereInFlags, and: () => import_ts_apicalypse2.and, exclude: () => import_ts_apicalypse2.exclude, fields: () => import_ts_apicalypse2.fields, igdb: () => igdb, limit: () => import_ts_apicalypse2.limit, multi: () => multi, offset: () => import_ts_apicalypse2.offset, or: () => import_ts_apicalypse2.or, request: () => request, search: () => import_ts_apicalypse2.search, sort: () => import_ts_apicalypse2.sort, twitchAccessToken: () => twitchAccessToken, where: () => import_ts_apicalypse2.where, whereIn: () => import_ts_apicalypse2.whereIn }); module.exports = __toCommonJS(index_exports); var import_ts_apicalypse = require("@phalcode/ts-apicalypse"); var import_axios2 = __toESM(require("axios"), 1); var import_ts_apicalypse2 = require("@phalcode/ts-apicalypse"); // src/twitch.ts var import_axios = __toESM(require("axios"), 1); var accessTokenUri = "https://id.twitch.tv/oauth2/token"; async function twitchAccessToken(params) { const searchParams = new URLSearchParams({ ...params, grant_type: "client_credentials" }); const url = `${accessTokenUri}?${searchParams}`; try { const response = await import_axios.default.post(url); if (response.data) { return response.data.access_token; } throw new Error("No data"); } catch (error) { console.error("Access Token error", error); throw error; } } // src/index.ts var BASE_URL = "https://api.igdb.com/v4"; var BASE_URL_MULTI = `${BASE_URL}/multiquery`; function buildUrl(key) { return `${BASE_URL}/${key}`; } function getFunctions(defaultHeaders = {}) { return { request(key) { const x = (0, import_ts_apicalypse.request)(); return { pipe(...steps) { const ex = x.pipe(...steps); return { ...ex, execute(options = {}) { return ex.execute.bind(ex)(buildUrl(key), { ...options, headers: { ...options?.headers, ...defaultHeaders } }); } }; }, alias(alias) { return x.sub(key, alias); } }; }, multi(...builders) { const ex = (0, import_ts_apicalypse.multi)(...builders); return { ...ex, execute(options = {}) { return ex.execute.bind(ex)(BASE_URL_MULTI, { ...options, headers: { ...options?.headers, ...defaultHeaders } }); } }; }, webhooks: { register(key, params, options = {}) { const encodedParams = new URLSearchParams( params ); return import_axios2.default.create()(buildUrl(`${key}/webhooks`), { method: "post", data: encodedParams.toString(), ...options, headers: { "content-type": "application/x-www-form-urlencoded", ...options?.headers, ...defaultHeaders } }); }, get(key, options = {}) { return import_axios2.default.create()(buildUrl(key ? `webhooks/${key}` : "webhooks"), { method: "get", ...options, headers: { ...options?.headers, ...defaultHeaders } }); }, delete(key, options = {}) { return import_axios2.default.create()(buildUrl(`webhooks/${key}`), { method: "delete", ...options, headers: { ...options?.headers, ...defaultHeaders } }); }, test(key, id, entityId, options = {}) { return import_axios2.default.post( buildUrl(`${key}/webhooks/test/${id}?entityId=${entityId}`), null, { ...options, headers: { ...options?.headers, ...defaultHeaders } } ); } } }; } var topLevelFns = getFunctions(); function request(key) { return topLevelFns.request(key); } function multi(...builders) { return topLevelFns.multi(...builders); } function igdb(clientId, accessToken) { const headers = { "client-id": clientId, authorization: `Bearer ${accessToken}` }; const wrappedFns = getFunctions(headers); return { request(key) { return wrappedFns.request(key); }, multi(...builders) { return wrappedFns.multi(...builders); }, webhooks: wrappedFns.webhooks }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { WhereFlags, WhereInFlags, and, exclude, fields, igdb, limit, multi, offset, or, request, search, sort, twitchAccessToken, where, whereIn });