@discordkit/core 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/dist/esm/DiscordSession.d.ts +16 -0
  2. package/dist/esm/DiscordSession.js +40 -0
  3. package/dist/esm/DiscordSession.js.map +1 -0
  4. package/dist/esm/addParams.d.ts +2 -0
  5. package/dist/esm/addParams.js +11 -0
  6. package/dist/esm/addParams.js.map +1 -0
  7. package/dist/esm/buildURL.d.ts +1 -0
  8. package/dist/esm/buildURL.js +4 -0
  9. package/dist/esm/buildURL.js.map +1 -0
  10. package/dist/esm/getAsset.d.ts +2 -0
  11. package/dist/esm/getAsset.js +3 -0
  12. package/dist/esm/getAsset.js.map +1 -0
  13. package/dist/esm/index.d.ts +8 -0
  14. package/dist/esm/index.js +9 -0
  15. package/dist/esm/index.js.map +1 -0
  16. package/dist/esm/isNonNullable.d.ts +19 -0
  17. package/dist/esm/isNonNullable.js +20 -0
  18. package/dist/esm/isNonNullable.js.map +1 -0
  19. package/dist/esm/isObject.d.ts +1 -0
  20. package/dist/esm/isObject.js +2 -0
  21. package/dist/esm/isObject.js.map +1 -0
  22. package/dist/esm/methods.d.ts +9 -0
  23. package/dist/esm/methods.js +8 -0
  24. package/dist/esm/methods.js.map +1 -0
  25. package/dist/esm/request.d.ts +2 -0
  26. package/dist/esm/request.js +21 -0
  27. package/dist/esm/request.js.map +1 -0
  28. package/dist/esm/snowflake.d.ts +2 -0
  29. package/dist/esm/snowflake.js +17 -0
  30. package/dist/esm/snowflake.js.map +1 -0
  31. package/dist/esm/toCamelCase.d.ts +1 -0
  32. package/dist/esm/toCamelCase.js +2 -0
  33. package/dist/esm/toCamelCase.js.map +1 -0
  34. package/dist/esm/toCamelKeys.d.ts +2 -0
  35. package/dist/esm/toCamelKeys.js +15 -0
  36. package/dist/esm/toCamelKeys.js.map +1 -0
  37. package/dist/esm/toProcedure.d.ts +35 -0
  38. package/dist/esm/toProcedure.js +26 -0
  39. package/dist/esm/toProcedure.js.map +1 -0
  40. package/dist/esm/toQuery.d.ts +9 -0
  41. package/dist/esm/toQuery.js +9 -0
  42. package/dist/esm/toQuery.js.map +1 -0
  43. package/dist/esm/toSnakeCase.d.ts +1 -0
  44. package/dist/esm/toSnakeCase.js +4 -0
  45. package/dist/esm/toSnakeCase.js.map +1 -0
  46. package/dist/esm/toSnakeKeys.d.ts +2 -0
  47. package/dist/esm/toSnakeKeys.js +15 -0
  48. package/dist/esm/toSnakeKeys.js.map +1 -0
  49. package/dist/esm/toValidated.d.ts +12 -0
  50. package/dist/esm/toValidated.js +23 -0
  51. package/dist/esm/toValidated.js.map +1 -0
  52. package/package.json +64 -0
@@ -0,0 +1,16 @@
1
+ export declare const endpoint = "https://discord.com/api/v10/";
2
+ /** @internal */
3
+ export declare class DiscordSession {
4
+ #private;
5
+ endpoint: string;
6
+ get ready(): boolean;
7
+ constructor(authToken?: string | null);
8
+ /**
9
+ * Clears the current session details, then attempts to set
10
+ * new session values
11
+ */
12
+ setToken: (token: string) => void;
13
+ clearSession: () => void;
14
+ getSession: () => string;
15
+ }
16
+ export declare const discord: DiscordSession;
@@ -0,0 +1,40 @@
1
+ export const endpoint = `https://discord.com/api/v10/`;
2
+ /** @internal */
3
+ export class DiscordSession {
4
+ endpoint = endpoint;
5
+ #authToken = null;
6
+ get ready() {
7
+ return Boolean(this.#authToken);
8
+ }
9
+ constructor(authToken) {
10
+ if (authToken) {
11
+ this.setToken(authToken);
12
+ }
13
+ }
14
+ /**
15
+ * Clears the current session details, then attempts to set
16
+ * new session values
17
+ */
18
+ setToken = (token) => {
19
+ this.#authToken = null;
20
+ if (!token) {
21
+ throw new Error(`Must provide a non-empty string to set Auth Token`);
22
+ }
23
+ if (!token.startsWith(`Bot `) && !token.startsWith(`Bearer `)) {
24
+ throw new Error(`Token must begin with either "Bot " or "Bearer ", received: ${token}`);
25
+ }
26
+ this.#authToken = token;
27
+ };
28
+ clearSession = () => {
29
+ this.#authToken = null;
30
+ };
31
+ getSession = () => {
32
+ const token = this.#authToken;
33
+ if (!token) {
34
+ throw new Error(`Auth Token must be set before requests can be made.`);
35
+ }
36
+ return token;
37
+ };
38
+ }
39
+ export const discord = new DiscordSession();
40
+ //# sourceMappingURL=DiscordSession.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DiscordSession.js","sourceRoot":"","sources":["../../src/DiscordSession.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,QAAQ,GAAG,8BAA8B,CAAC;AAEvD,gBAAgB;AAChB,MAAM,OAAO,cAAc;IACzB,QAAQ,GAAW,QAAQ,CAAC;IAC5B,UAAU,GAAkB,IAAI,CAAC;IACjC,IAAI,KAAK;QACP,OAAO,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IAClC,CAAC;IAED,YAAY,SAAyB;QACnC,IAAI,SAAS,EAAE;YACb,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC;SAC1B;IACH,CAAC;IAED;;;OAGG;IACH,QAAQ,GAAG,CAAC,KAAa,EAAQ,EAAE;QACjC,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC;QACvB,IAAI,CAAC,KAAK,EAAE;YACV,MAAM,IAAI,KAAK,CAAC,mDAAmD,CAAC,CAAC;SACtE;QACD,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE;YAC7D,MAAM,IAAI,KAAK,CACb,+DAA+D,KAAK,EAAE,CACvE,CAAC;SACH;QACD,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC;IAC1B,CAAC,CAAC;IAEF,YAAY,GAAG,GAAS,EAAE;QACxB,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC;IACzB,CAAC,CAAC;IAEF,UAAU,GAAG,GAAW,EAAE;QACxB,MAAM,KAAK,GAAG,IAAI,CAAC,UAAU,CAAC;QAE9B,IAAI,CAAC,KAAK,EAAE;YACV,MAAM,IAAI,KAAK,CAAC,qDAAqD,CAAC,CAAC;SACxE;QAED,OAAO,KAAK,CAAC;IACf,CAAC,CAAC;CACH;AAED,MAAM,CAAC,MAAM,OAAO,GAAG,IAAI,cAAc,EAAE,CAAC"}
@@ -0,0 +1,2 @@
1
+ export type RequestParams = Partial<Record<string, number[] | string[] | boolean | number | string | null | undefined>>;
2
+ export declare const addParams: <P extends Partial<Record<string, string | number | boolean | string[] | number[] | null | undefined>>>(url: URL, params: P) => URL;
@@ -0,0 +1,11 @@
1
+ import { isNonNullable } from "./isNonNullable.js";
2
+ import { toSnakeCase } from "./toSnakeCase.js";
3
+ export const addParams = (url, params) => {
4
+ for (const [key, value] of Object.entries(params)) {
5
+ if (isNonNullable(value)) {
6
+ url.searchParams.set(toSnakeCase(key), value.toString());
7
+ }
8
+ }
9
+ return url;
10
+ };
11
+ //# sourceMappingURL=addParams.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"addParams.js","sourceRoot":"","sources":["../../src/addParams.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAS/C,MAAM,CAAC,MAAM,SAAS,GAAG,CACvB,GAAQ,EACR,MAAS,EACJ,EAAE;IACP,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;QACjD,IAAI,aAAa,CAAC,KAAK,CAAC,EAAE;YACxB,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC;SAC1D;KACF;IAED,OAAO,GAAG,CAAC;AACb,CAAC,CAAC"}
@@ -0,0 +1 @@
1
+ export declare const buildURL: <P extends Partial<Record<string, string | number | boolean | string[] | number[] | null | undefined>>>(resource: string, params?: P | undefined, base?: string) => URL;
@@ -0,0 +1,4 @@
1
+ import { endpoint } from "./DiscordSession.js";
2
+ import { addParams } from "./addParams.js";
3
+ export const buildURL = (resource, params, base) => addParams(new URL(resource.replace(/^\//, ``), base ?? endpoint), params ?? {});
4
+ //# sourceMappingURL=buildURL.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"buildURL.js","sourceRoot":"","sources":["../../src/buildURL.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAsB,MAAM,gBAAgB,CAAC;AAE/D,MAAM,CAAC,MAAM,QAAQ,GAAG,CACtB,QAAgB,EAChB,MAAU,EACV,IAAa,EACR,EAAE,CACP,SAAS,CACP,IAAI,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,EAAE,IAAI,IAAI,QAAQ,CAAC,EACtD,MAAM,IAAI,EAAE,CACb,CAAC"}
@@ -0,0 +1,2 @@
1
+ import { buildURL } from "./buildURL.js";
2
+ export declare const getAsset: (resource: string, params?: Parameters<typeof buildURL>[1]) => string;
@@ -0,0 +1,3 @@
1
+ import { buildURL } from "./buildURL.js";
2
+ export const getAsset = (resource, params) => buildURL(resource, params, `https://cdn.discordapp.com/`).href;
3
+ //# sourceMappingURL=getAsset.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getAsset.js","sourceRoot":"","sources":["../../src/getAsset.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzC,MAAM,CAAC,MAAM,QAAQ,GAAG,CACtB,QAAgB,EAChB,MAAuC,EAC/B,EAAE,CAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,6BAA6B,CAAC,CAAC,IAAI,CAAC"}
@@ -0,0 +1,8 @@
1
+ export { endpoint, discord } from "./DiscordSession.js";
2
+ export * from "./buildURL.js";
3
+ export * from "./getAsset.js";
4
+ export * from "./methods.js";
5
+ export * from "./toProcedure.js";
6
+ export * from "./toQuery.js";
7
+ export * from "./toValidated.js";
8
+ export * from "./snowflake.js";
@@ -0,0 +1,9 @@
1
+ export { endpoint, discord } from "./DiscordSession.js";
2
+ export * from "./buildURL.js";
3
+ export * from "./getAsset.js";
4
+ export * from "./methods.js";
5
+ export * from "./toProcedure.js";
6
+ export * from "./toQuery.js";
7
+ export * from "./toValidated.js";
8
+ export * from "./snowflake.js";
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AACxD,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Used to test whether a `Maybe` typed value is `null` or `undefined`.
3
+ *
4
+ * When called, the given value's type is narrowed to `NonNullable<T>`.
5
+ *
6
+ * ### Example Usage:
7
+ *
8
+ * ```ts
9
+ * const fn = (str: Maybe<string>) => {
10
+ * if (!isNonNullable(str)) {
11
+ * // typeof str = null | undefined
12
+ * // ...
13
+ * }
14
+ * // typeof str = string
15
+ * // ...
16
+ * }
17
+ * ```
18
+ */
19
+ export declare const isNonNullable: <T extends unknown>(val?: T | undefined) => val is NonNullable<T>;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Used to test whether a `Maybe` typed value is `null` or `undefined`.
3
+ *
4
+ * When called, the given value's type is narrowed to `NonNullable<T>`.
5
+ *
6
+ * ### Example Usage:
7
+ *
8
+ * ```ts
9
+ * const fn = (str: Maybe<string>) => {
10
+ * if (!isNonNullable(str)) {
11
+ * // typeof str = null | undefined
12
+ * // ...
13
+ * }
14
+ * // typeof str = string
15
+ * // ...
16
+ * }
17
+ * ```
18
+ */
19
+ export const isNonNullable = (val) => typeof val !== `undefined` && val !== null;
20
+ //# sourceMappingURL=isNonNullable.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"isNonNullable.js","sourceRoot":"","sources":["../../src/isNonNullable.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAC3B,GAAO,EACgB,EAAE,CAAC,OAAO,GAAG,KAAK,WAAW,IAAI,GAAG,KAAK,IAAI,CAAC"}
@@ -0,0 +1 @@
1
+ export declare const isObject: (val: unknown) => val is object;
@@ -0,0 +1,2 @@
1
+ export const isObject = (val) => Boolean(val) && typeof val === `object` && !Array.isArray(val);
2
+ //# sourceMappingURL=isObject.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"isObject.js","sourceRoot":"","sources":["../../src/isObject.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,GAAY,EAAiB,EAAE,CACtD,OAAO,CAAC,GAAG,CAAC,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC"}
@@ -0,0 +1,9 @@
1
+ import type { z } from "zod";
2
+ import type { RequestParams } from "./addParams.js";
3
+ import type { RequestBody } from "./request.js";
4
+ export type Fetcher<S extends z.ZodTypeAny | null = null, R = void> = S extends null ? () => Promise<R> : (config: z.infer<NonNullable<S>>) => Promise<R>;
5
+ export declare const get: <T>(url: string, params?: RequestParams) => Promise<T>;
6
+ export declare const post: <T>(url: string, body?: RequestBody) => Promise<T>;
7
+ export declare const put: <T>(url: string, body?: RequestBody) => Promise<T>;
8
+ export declare const patch: <T>(url: string, body?: RequestBody) => Promise<T>;
9
+ export declare const remove: <T = void>(url: string) => Promise<T>;
@@ -0,0 +1,8 @@
1
+ import { buildURL } from "./buildURL.js";
2
+ import { request } from "./request.js";
3
+ export const get = async (url, params) => request(buildURL(url, params));
4
+ export const post = async (url, body) => request(buildURL(url), `POST`, body);
5
+ export const put = async (url, body) => request(buildURL(url), `PUT`, body);
6
+ export const patch = async (url, body) => request(buildURL(url), `PATCH`, body);
7
+ export const remove = async (url) => request(buildURL(url), `DELETE`);
8
+ //# sourceMappingURL=methods.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"methods.js","sourceRoot":"","sources":["../../src/methods.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AASvC,MAAM,CAAC,MAAM,GAAG,GAAG,KAAK,EAAK,GAAW,EAAE,MAAsB,EAAc,EAAE,CAC9E,OAAO,CAAC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC,CAAC;AAEjC,MAAM,CAAC,MAAM,IAAI,GAAG,KAAK,EAAK,GAAW,EAAE,IAAkB,EAAc,EAAE,CAC3E,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;AAEvC,MAAM,CAAC,MAAM,GAAG,GAAG,KAAK,EAAK,GAAW,EAAE,IAAkB,EAAc,EAAE,CAC1E,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;AAEtC,MAAM,CAAC,MAAM,KAAK,GAAG,KAAK,EAAK,GAAW,EAAE,IAAkB,EAAc,EAAE,CAC5E,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;AAExC,MAAM,CAAC,MAAM,MAAM,GAAG,KAAK,EAAY,GAAW,EAAc,EAAE,CAChE,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export type RequestBody = object | null | undefined;
2
+ export declare const request: <T>(resource: URL, method?: string, body?: RequestBody) => Promise<T>;
@@ -0,0 +1,21 @@
1
+ import { discord } from "./DiscordSession.js";
2
+ import { toCamelKeys } from "./toCamelKeys.js";
3
+ import { toSnakeKeys } from "./toSnakeKeys.js";
4
+ export const request = async (resource, method = `GET`, body) => {
5
+ const token = discord.getSession();
6
+ if (!token) {
7
+ throw new Error(`Auth Token must be set before requests can be made.`);
8
+ }
9
+ const res = await fetch(resource.toString(), {
10
+ method,
11
+ body: body ? JSON.stringify(toSnakeKeys(body)) : body,
12
+ headers: {
13
+ Authorization: token
14
+ }
15
+ });
16
+ if (!res.ok) {
17
+ throw new Error(`Request to resource '${resource.toString()}' failed:\n\n${res.statusText}`);
18
+ }
19
+ return toCamelKeys(await res.json());
20
+ };
21
+ //# sourceMappingURL=request.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request.js","sourceRoot":"","sources":["../../src/request.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAC9C,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAI/C,MAAM,CAAC,MAAM,OAAO,GAAG,KAAK,EAC1B,QAAa,EACb,MAAM,GAAG,KAAK,EACd,IAAkB,EACN,EAAE;IACd,MAAM,KAAK,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC;IAEnC,IAAI,CAAC,KAAK,EAAE;QACV,MAAM,IAAI,KAAK,CAAC,qDAAqD,CAAC,CAAC;KACxE;IAED,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,QAAQ,CAAC,QAAQ,EAAE,EAAE;QAC3C,MAAM;QACN,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI;QACrD,OAAO,EAAE;YACP,aAAa,EAAE,KAAK;SACrB;KACF,CAAC,CAAC;IAEH,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE;QACX,MAAM,IAAI,KAAK,CACb,wBAAwB,QAAQ,CAAC,QAAQ,EAAE,gBACzC,GAAG,CAAC,UACN,EAAE,CACH,CAAC;KACH;IAED,OAAO,WAAW,CAAC,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC;AACvC,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ import { z } from "zod";
2
+ export declare const snowflake: z.ZodType<string, z.ZodTypeDef, string>;
@@ -0,0 +1,17 @@
1
+ import { z } from "zod";
2
+ import { isNonNullable } from "./isNonNullable.js";
3
+ export const snowflake = z
4
+ .custom((val) => {
5
+ if (isNonNullable(val) &&
6
+ (typeof val === `bigint` ||
7
+ typeof val === `number` ||
8
+ typeof val === `string`)) {
9
+ return (z.coerce
10
+ .date()
11
+ // eslint-disable-next-line no-bitwise
12
+ .safeParse(Number((BigInt(val) >> 22n) + 1420070400000n)));
13
+ }
14
+ return false;
15
+ }, `Invalid Snowflake ID`)
16
+ .describe(`Discord Snowflake ID`);
17
+ //# sourceMappingURL=snowflake.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"snowflake.js","sourceRoot":"","sources":["../../src/snowflake.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEnD,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC;KACvB,MAAM,CAAS,CAAC,GAAG,EAAE,EAAE;IACtB,IACE,aAAa,CAAC,GAAG,CAAC;QAClB,CAAC,OAAO,GAAG,KAAK,QAAQ;YACtB,OAAO,GAAG,KAAK,QAAQ;YACvB,OAAO,GAAG,KAAK,QAAQ,CAAC,EAC1B;QACA,OAAO,CACL,CAAC,CAAC,MAAM;aACL,IAAI,EAAE;YACP,sCAAsC;aACrC,SAAS,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,GAAG,cAAc,CAAC,CAAC,CAC5D,CAAC;KACH;IACD,OAAO,KAAK,CAAC;AACf,CAAC,EAAE,sBAAsB,CAAC;KACzB,QAAQ,CAAC,sBAAsB,CAAC,CAAC"}
@@ -0,0 +1 @@
1
+ export declare const toCamelCase: (str: string) => string;
@@ -0,0 +1,2 @@
1
+ export const toCamelCase = (str) => str.replace(/_(?<char>[a-zA-Z])/g, (g) => g[1].toUpperCase());
2
+ //# sourceMappingURL=toCamelCase.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toCamelCase.js","sourceRoot":"","sources":["../../src/toCamelCase.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,GAAW,EAAU,EAAE,CACjD,GAAG,CAAC,OAAO,CAAC,qBAAqB,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ import type { CamelCasedPropertiesDeep } from "type-fest";
2
+ export declare const toCamelKeys: <T extends object>(o: T) => CamelCasedPropertiesDeep<T>;
@@ -0,0 +1,15 @@
1
+ import { isObject } from "./isObject.js";
2
+ import { toCamelCase } from "./toCamelCase.js";
3
+ export const toCamelKeys = (o) => {
4
+ if (Array.isArray(o)) {
5
+ return o.map(toCamelKeys);
6
+ }
7
+ else if (isObject(o)) {
8
+ return Object.entries(o).reduce((acc, [key, value]) => {
9
+ acc[toCamelCase(key)] = toCamelKeys(value);
10
+ return acc;
11
+ }, {});
12
+ }
13
+ return o;
14
+ };
15
+ //# sourceMappingURL=toCamelKeys.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toCamelKeys.js","sourceRoot":"","sources":["../../src/toCamelKeys.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C,MAAM,CAAC,MAAM,WAAW,GAAG,CACzB,CAAI,EACyB,EAAE;IAC/B,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE;QACpB,OAAO,CAAC,CAAC,GAAG,CAAC,WAAW,CAAgC,CAAC;KAC1D;SAAM,IAAI,QAAQ,CAAC,CAAC,CAAC,EAAE;QACtB,OAAO,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE;YACpD,GAAG,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,GAAG,WAAW,CAAC,KAAK,CAAC,CAAC;YAC3C,OAAO,GAAG,CAAC;QACb,CAAC,EAAE,EAAE,CAAgC,CAAC;KACvC;IAED,OAAO,CAAC,CAAC;AACX,CAAC,CAAC"}
@@ -0,0 +1,35 @@
1
+ import type { z } from "zod";
2
+ import type { AnyRootConfig, inferParser, Procedure, unsetMarker } from "@trpc/server";
3
+ import type { Fetcher } from "./methods.js";
4
+ type Result<T = void> = T extends z.ZodTypeAny ? z.infer<T> : T;
5
+ type UnsetMarker = typeof unsetMarker;
6
+ type BaseProcedure<T extends "mutation" | "query" | "subscription", I extends z.ZodTypeAny | null = null, O extends z.ZodTypeAny | null = null> = Procedure<T, {
7
+ _config: AnyRootConfig;
8
+ _ctx_out: object;
9
+ _input_in: I extends z.ZodTypeAny ? inferParser<I>["in"] : UnsetMarker;
10
+ _input_out: I extends z.ZodTypeAny ? inferParser<I>["out"] : UnsetMarker;
11
+ _output_in: O extends z.ZodTypeAny ? inferParser<O>["in"] : UnsetMarker;
12
+ _output_out: O extends z.ZodTypeAny ? inferParser<O>["out"] : UnsetMarker;
13
+ _meta: object;
14
+ }>;
15
+ /**
16
+ * Given a {@link Fetcher | Fetcher} function and it's associated input and
17
+ * output Zod schemas, this produces a function which accepts a tRPC procedure
18
+ * builder of the given procedure type. This can then be used in a tRPC router
19
+ * to scaffold an API route to forward a request to Discord's API.
20
+ */
21
+ export declare const toProcedure: <T extends "query" | "mutation" | "subscription", I extends z.ZodTypeAny | null = null, O extends z.ZodTypeAny | null = null>(type: T, fn: Fetcher<I extends z.ZodTypeAny ? I : z.ZodUnknown, Result<O>>, input?: I | undefined, output?: O | undefined) => (procedure: import("@trpc/server").ProcedureBuilder<{
22
+ _config: import("@trpc/server").RootConfig<{
23
+ ctx: object;
24
+ meta: object;
25
+ errorShape: import("@trpc/server").DefaultErrorShape;
26
+ transformer: import("@trpc/server").DefaultDataTransformer;
27
+ }>;
28
+ _ctx_out: object;
29
+ _input_in: typeof unsetMarker;
30
+ _input_out: typeof unsetMarker;
31
+ _output_in: typeof unsetMarker;
32
+ _output_out: typeof unsetMarker;
33
+ _meta: object;
34
+ }>) => BaseProcedure<T, I, O>;
35
+ export {};
@@ -0,0 +1,26 @@
1
+ import { isNonNullable } from "./isNonNullable.js";
2
+ /**
3
+ * Given a {@link Fetcher | Fetcher} function and it's associated input and
4
+ * output Zod schemas, this produces a function which accepts a tRPC procedure
5
+ * builder of the given procedure type. This can then be used in a tRPC router
6
+ * to scaffold an API route to forward a request to Discord's API.
7
+ */
8
+ export const toProcedure = (type, fn, input, output) => (procedure) => {
9
+ if (isNonNullable(input) && isNonNullable(output)) {
10
+ // @ts-expect-error
11
+ return procedure
12
+ .input(input)
13
+ .output(output)[type](async (opts) => fn(opts.input));
14
+ }
15
+ if (isNonNullable(input) && !isNonNullable(output)) {
16
+ // @ts-expect-error
17
+ return procedure.input(input)[type](async (opts) => fn(opts.input));
18
+ }
19
+ if (!isNonNullable(input) && isNonNullable(output)) {
20
+ // @ts-expect-error
21
+ return procedure.output(output)[type](async () => fn());
22
+ }
23
+ // @ts-expect-error
24
+ return procedure[type](async () => fn());
25
+ };
26
+ //# sourceMappingURL=toProcedure.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toProcedure.js","sourceRoot":"","sources":["../../src/toProcedure.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AA0BnD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,WAAW,GACtB,CAKE,IAAO,EACP,EAAiE,EACjE,KAAS,EACT,MAAU,EACV,EAAE,CACJ,CAAC,SAA2B,EAA0B,EAAE;IACtD,IAAI,aAAa,CAAC,KAAK,CAAC,IAAI,aAAa,CAAC,MAAM,CAAC,EAAE;QACjD,mBAAmB;QACnB,OAAO,SAAS;aACb,KAAK,CAAC,KAAK,CAAC;aACZ,MAAM,CAAC,MAAM,CAAC,CACd,IAAI,CAAC,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;KAC1C;IACD,IAAI,aAAa,CAAC,KAAK,CAAC,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,EAAE;QAClD,mBAAmB;QACnB,OAAO,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;KACrE;IACD,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,aAAa,CAAC,MAAM,CAAC,EAAE;QAClD,mBAAmB;QACnB,OAAO,SAAS,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;KACzD;IACD,mBAAmB;IACnB,OAAO,SAAS,CAAC,IAAI,CAAC,CAAC,KAAK,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;AAC3C,CAAC,CAAC"}
@@ -0,0 +1,9 @@
1
+ import type { QueryFunction } from "@tanstack/react-query";
2
+ import type { z } from "zod";
3
+ import type { Fetcher } from "./methods.js";
4
+ /**
5
+ * Given a {@link Fetcher | Fetcher} function, transforms it into a curried function
6
+ * which can then be used with React-Query as a query function without
7
+ * the need for any additional boilerplate.
8
+ */
9
+ export declare const toQuery: <S extends z.ZodTypeAny | null, R, T extends Fetcher<S, R>>(fn: T) => Parameters<T>["length"] extends 0 ? () => QueryFunction<Awaited<ReturnType<T>>> : (config: Parameters<T>[0]) => QueryFunction<Awaited<ReturnType<T>>>;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Given a {@link Fetcher | Fetcher} function, transforms it into a curried function
3
+ * which can then be used with React-Query as a query function without
4
+ * the need for any additional boilerplate.
5
+ */
6
+ export const toQuery = (fn) =>
7
+ // @ts-expect-error
8
+ (...config) => async () => fn(...config);
9
+ //# sourceMappingURL=toQuery.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toQuery.js","sourceRoot":"","sources":["../../src/toQuery.ts"],"names":[],"mappings":"AAIA;;;;GAIG;AACH,MAAM,CAAC,MAAM,OAAO,GAClB,CACE,EAAK,EAGiE,EAAE;AAC1E,mBAAmB;AACnB,CAAC,GAAG,MAAiB,EAAE,EAAE,CACzB,KAAK,IAAI,EAAE,CACT,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC"}
@@ -0,0 +1 @@
1
+ export declare const toSnakeCase: (str: string) => string;
@@ -0,0 +1,4 @@
1
+ export const toSnakeCase = (str) => str
2
+ .replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`)
3
+ .substring(str.slice(0, 1).match(/(?<char>[A-Z])/g) ? 1 : 0);
4
+ //# sourceMappingURL=toSnakeCase.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toSnakeCase.js","sourceRoot":"","sources":["../../src/toSnakeCase.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,GAAW,EAAU,EAAE,CACjD,GAAG;KACA,OAAO,CAAC,QAAQ,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,MAAM,CAAC,WAAW,EAAE,EAAE,CAAC;KACzD,SAAS,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ import type { SnakeCasedPropertiesDeep } from "type-fest";
2
+ export declare const toSnakeKeys: <T extends object>(o: T) => SnakeCasedPropertiesDeep<T>;
@@ -0,0 +1,15 @@
1
+ import { isObject } from "./isObject.js";
2
+ import { toSnakeCase } from "./toSnakeCase.js";
3
+ export const toSnakeKeys = (o) => {
4
+ if (Array.isArray(o)) {
5
+ return o.map(toSnakeKeys);
6
+ }
7
+ else if (isObject(o)) {
8
+ return Object.entries(o).reduce((acc, [key, value]) => {
9
+ acc[toSnakeCase(key)] = toSnakeKeys(value);
10
+ return acc;
11
+ }, {});
12
+ }
13
+ return o;
14
+ };
15
+ //# sourceMappingURL=toSnakeKeys.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toSnakeKeys.js","sourceRoot":"","sources":["../../src/toSnakeKeys.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C,MAAM,CAAC,MAAM,WAAW,GAAG,CACzB,CAAI,EACyB,EAAE;IAC/B,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE;QACpB,OAAO,CAAC,CAAC,GAAG,CAAC,WAAW,CAAgC,CAAC;KAC1D;SAAM,IAAI,QAAQ,CAAC,CAAC,CAAC,EAAE;QACtB,OAAO,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE;YACpD,GAAG,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,GAAG,WAAW,CAAC,KAAK,CAAC,CAAC;YAC3C,OAAO,GAAG,CAAC;QACb,CAAC,EAAE,EAAE,CAAgC,CAAC;KACvC;IAED,OAAO,CAAC,CAAC;AACX,CAAC,CAAC"}
@@ -0,0 +1,12 @@
1
+ import type { z } from "zod";
2
+ import type { Fetcher } from "./methods.js";
3
+ type ToValidated = <F extends Fetcher<null, null> | Fetcher<null, z.infer<z.ZodTypeAny>> | Fetcher<z.ZodTypeAny, z.infer<z.ZodTypeAny>> | Fetcher<z.ZodTypeAny>>(...args: F extends Fetcher<infer I, ReturnType<F> extends Promise<void> ? never : z.infer<infer O & z.ZodTypeAny>> ? [fn: F, input: I, output: O] : F extends Fetcher<null, ReturnType<F> extends Promise<void> ? never : z.infer<infer O & z.ZodTypeAny>> ? [fn: F, input: null, output: O] : F extends Fetcher<infer I> ? [fn: F, input: I] : F extends Fetcher<null, null> ? [fn: F] : never) => F;
4
+ /**
5
+ * Given a {@link Fetcher | Fetcher} function and it's associated input
6
+ * and output Zod schemas, this returns a new validated {@link Fetcher | Fetcher} function which will validate it's input and result at runtime.
7
+ * This is useful in contexts where you want strong guarantees on runtime
8
+ * type-safety when dealing with raw user input in a framework agnostic
9
+ * environment.
10
+ */
11
+ export declare const toValidated: ToValidated;
12
+ export {};
@@ -0,0 +1,23 @@
1
+ import { isNonNullable } from "./isNonNullable.js";
2
+ import { isObject } from "./isObject.js";
3
+ const isSchema = (val) => isNonNullable(val) && isObject(val) && `parse` in val;
4
+ /**
5
+ * Given a {@link Fetcher | Fetcher} function and it's associated input
6
+ * and output Zod schemas, this returns a new validated {@link Fetcher | Fetcher} function which will validate it's input and result at runtime.
7
+ * This is useful in contexts where you want strong guarantees on runtime
8
+ * type-safety when dealing with raw user input in a framework agnostic
9
+ * environment.
10
+ */
11
+ export const toValidated = (fn, input, output) =>
12
+ // @ts-expect-error
13
+ async (config) => {
14
+ if (isSchema(input)) {
15
+ input.parse(config);
16
+ }
17
+ const result = await fn(config);
18
+ if (isSchema(output)) {
19
+ output.parse(result);
20
+ }
21
+ return result;
22
+ };
23
+ //# sourceMappingURL=toValidated.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toValidated.js","sourceRoot":"","sources":["../../src/toValidated.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAGzC,MAAM,QAAQ,GAAG,CAAC,GAAY,EAAuB,EAAE,CACrD,aAAa,CAAC,GAAG,CAAC,IAAI,QAAQ,CAAC,GAAG,CAAC,IAAI,OAAO,IAAI,GAAG,CAAC;AA8BxD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,WAAW,GACtB,CAAC,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE;AACtB,mBAAmB;AACnB,KAAK,EAAE,MAAM,EAAE,EAAE;IACf,IAAI,QAAQ,CAAC,KAAK,CAAC,EAAE;QACnB,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;KACrB;IAED,MAAM,MAAM,GAAG,MAAM,EAAE,CAAC,MAAM,CAAC,CAAC;IAEhC,IAAI,QAAQ,CAAC,MAAM,CAAC,EAAE;QACpB,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;KACtB;IAED,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@discordkit/core",
3
+ "description": "Core utility functions for Discordkit",
4
+ "version": "1.0.0",
5
+ "license": "MIT",
6
+ "author": "Drake Costa <drake@saeris.gg> (https://saeris.gg)",
7
+ "keywords": [
8
+ "discordkit",
9
+ "core",
10
+ "itilities"
11
+ ],
12
+ "homepage": "https://github.com/discordkit/discordkit#readme",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/discordkit/discordkit.git",
16
+ "directory": "packages/core"
17
+ },
18
+ "publishConfig": {
19
+ "access": "public",
20
+ "registry": "https://registry.npmjs.org"
21
+ },
22
+ "type": "module",
23
+ "main": "./dist/cjs/index.js",
24
+ "module": "./dist/esm/index.js",
25
+ "types": "./dist/esm/index.d.ts",
26
+ "exports": {
27
+ ".": {
28
+ "types": "./dist/esm/index.d.ts",
29
+ "module": "./dist/esm/index.js",
30
+ "import": "./dist/esm/index.js",
31
+ "require": {
32
+ "types": "./dist/cjs/index.d.ts",
33
+ "default": "./dist/cjs/index.js"
34
+ },
35
+ "default": "./dist/esm/index.js"
36
+ },
37
+ "./*": {
38
+ "types": "./dist/esm/*.d.ts",
39
+ "module": "./dist/esm/*.js",
40
+ "import": "./dist/esm/*.js",
41
+ "require": {
42
+ "types": "./dist/cjs/*.d.ts",
43
+ "default": "./dist/cjs/*.js"
44
+ },
45
+ "default": "./dist/esm/*.js"
46
+ },
47
+ "./package.json": "./package.json"
48
+ },
49
+ "files": [
50
+ "dist/**/*"
51
+ ],
52
+ "sideEffects": false,
53
+ "scripts": {
54
+ "dev": "yarn build --watch",
55
+ "build": "tsc --project ./tsconfig.build.json",
56
+ "build:cjs": "tsc --project ./tsconfig.build.json --outdir dist/cjs --module commonjs --moduleresolution node"
57
+ },
58
+ "peerDependencies": {
59
+ "zod": "^3.17.3"
60
+ },
61
+ "devDependencies": {
62
+ "typescript": "^5.2.2"
63
+ }
64
+ }