@phalcode/ts-igdb-client
Version:
A Typescript client and request builder for IGDB using ts-apicalypse
182 lines (179 loc) • 4.03 kB
JavaScript
// src/index.ts
import {
multi as multiA,
request as requestA
} from "@phalcode/ts-apicalypse";
import axios2 from "axios";
import {
WhereFlags,
WhereInFlags,
and,
exclude,
fields,
limit,
offset,
or,
search,
sort,
where,
whereIn
} from "@phalcode/ts-apicalypse";
// src/twitch.ts
import axios from "axios";
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 axios.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 = requestA();
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 = multiA(...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 axios2.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 axios2.create()(buildUrl(key ? `webhooks/${key}` : "webhooks"), {
method: "get",
...options,
headers: {
...options?.headers,
...defaultHeaders
}
});
},
delete(key, options = {}) {
return axios2.create()(buildUrl(`webhooks/${key}`), {
method: "delete",
...options,
headers: {
...options?.headers,
...defaultHeaders
}
});
},
test(key, id, entityId, options = {}) {
return axios2.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
};
}
export {
WhereFlags,
WhereInFlags,
and,
exclude,
fields,
igdb,
limit,
multi,
offset,
or,
request,
search,
sort,
twitchAccessToken,
where,
whereIn
};