@squarecloud/api
Version:
A NodeJS wrapper for Square Cloud API
1 lines • 11.5 kB
Source Map (JSON)
{"version":3,"sources":["../../src/modules/deploys.ts","../../src/lib/routes.ts","../../src/structures/deploy.ts","../../src/structures/error.ts","../../src/assertions/common.ts","../../src/assertions/literal.ts"],"sourcesContent":["import { assertString } from \"@/assertions/literal\";\nimport { Routes } from \"@/lib/routes\";\nimport type { BaseApplication } from \"@/structures\";\nimport { Deployment } from \"@/structures/deploy\";\n\nexport class DeploysModule {\n\tconstructor(public readonly application: BaseApplication) {}\n\n\t/**\n\t * Integrates Square Cloud with GitHub webhooks\n\t *\n\t * @param accessToken - The access token for your GitHub repository. You can find this in your [GitHub Tokens Classic](https://github.com/settings/tokens/new)\n\t */\n\tasync integrateGithubWebhook(accessToken: string) {\n\t\tassertString(accessToken);\n\n\t\tconst data = await this.application.client.api.request(\n\t\t\tRoutes.apps.deployments.webhook(this.application.id),\n\t\t\t{ method: \"POST\", body: { access_token: accessToken } },\n\t\t);\n\n\t\treturn data.response.webhook;\n\t}\n\n\t/**\n\t * Gets the last 10 deployments of an application from the last 24 hours\n\t */\n\tasync list() {\n\t\tconst data = await this.application.client.api.request(\n\t\t\tRoutes.apps.deployments.list(this.application.id),\n\t\t);\n\n\t\treturn data.response.map(\n\t\t\t(deployment) => new Deployment(this.application, deployment),\n\t\t);\n\t}\n\n\t/**\n\t * Gets the current webhook URL\n\t */\n\tasync webhookURL() {\n\t\tconst data = await this.application.client.api.request(\n\t\t\tRoutes.apps.deployments.current(this.application.id),\n\t\t);\n\n\t\treturn data.response.webhook;\n\t}\n}\n","import type { APIEndpoint } from \"@/types\";\n\nexport type Route<T extends APIEndpoint> = string & { __route: T };\nexport const Route = <T extends APIEndpoint>(route: string) =>\n\troute as Route<T>;\n\ninterface IRoutes {\n\t[k: string]: ((...args: string[]) => Route<APIEndpoint>) | IRoutes;\n}\n\nexport const Routes = {\n\tuser: () => {\n\t\treturn Route<\"user\">(\"users/me\");\n\t},\n\tservice: {\n\t\tstatus: () => {\n\t\t\treturn Route<\"service/status\">(\"service/status\");\n\t\t},\n\t},\n\tapps: {\n\t\tupload: () => {\n\t\t\treturn Route<\"apps/upload\">(\"apps\");\n\t\t},\n\t\tstatusAll: () => {\n\t\t\treturn Route<\"apps/status-all\">(\"apps/status\");\n\t\t},\n\t\tinfo: (appId: string) => {\n\t\t\treturn Route<\"apps/info\">(`apps/${appId}`);\n\t\t},\n\t\tstatus: (appId: string) => {\n\t\t\treturn Route<\"apps/status\">(`apps/${appId}/status`);\n\t\t},\n\t\tlogs: (appId: string) => {\n\t\t\treturn Route<\"apps/logs\">(`apps/${appId}/logs`);\n\t\t},\n\t\tdelete: (appId: string) => {\n\t\t\treturn Route<\"apps/delete\">(`apps/${appId}`);\n\t\t},\n\t\tcommit: (appId: string) => {\n\t\t\treturn Route<\"apps/commit\">(`apps/${appId}/commit`);\n\t\t},\n\t\tsnapshots: (appId: string) => {\n\t\t\treturn Route<\"apps/snapshots\">(`apps/${appId}/snapshots`);\n\t\t},\n\t\tgenerateSnapshot: (appId: string) => {\n\t\t\treturn Route<\"apps/generate-snapshot\">(`apps/${appId}/snapshots`);\n\t\t},\n\t\tstart: (appId: string) => {\n\t\t\treturn Route<\"apps/start\">(`apps/${appId}/start`);\n\t\t},\n\t\trestart: (appId: string) => {\n\t\t\treturn Route<\"apps/restart\">(`apps/${appId}/restart`);\n\t\t},\n\t\tstop: (appId: string) => {\n\t\t\treturn Route<\"apps/stop\">(`apps/${appId}/stop`);\n\t\t},\n\t\tfiles: {\n\t\t\tread: (appId: string) => {\n\t\t\t\treturn Route<\"apps/files/read\">(`apps/${appId}/files/content`);\n\t\t\t},\n\t\t\tlist: (appId: string) => {\n\t\t\t\treturn Route<\"apps/files/list\">(`apps/${appId}/files`);\n\t\t\t},\n\t\t\tupsert: (appId: string) => {\n\t\t\t\treturn Route<\"apps/files/upsert\">(`apps/${appId}/files`);\n\t\t\t},\n\t\t\tmove: (appId: string) => {\n\t\t\t\treturn Route<\"apps/files/move\">(`apps/${appId}/files`);\n\t\t\t},\n\t\t\tdelete: (appId: string) => {\n\t\t\t\treturn Route<\"apps/files/delete\">(`apps/${appId}/files`);\n\t\t\t},\n\t\t},\n\t\tdeployments: {\n\t\t\tlist: (appId: string) => {\n\t\t\t\treturn Route<\"apps/deployments/list\">(`apps/${appId}/deployments`);\n\t\t\t},\n\t\t\tcurrent: (appId: string) => {\n\t\t\t\treturn Route<\"apps/deployments/current\">(\n\t\t\t\t\t`apps/${appId}/deployments/current`,\n\t\t\t\t);\n\t\t\t},\n\t\t\twebhook: (appId: string) => {\n\t\t\t\treturn Route<\"apps/deployments/webhook\">(\n\t\t\t\t\t`apps/${appId}/deploy/webhook`,\n\t\t\t\t);\n\t\t\t},\n\t\t},\n\t\tnetwork: {\n\t\t\tdns: (appId: string) => {\n\t\t\t\treturn Route<\"apps/network/dns\">(`apps/${appId}/network/dns`);\n\t\t\t},\n\t\t\tcustom: (appId: string) => {\n\t\t\t\treturn Route<\"apps/network/custom\">(`apps/${appId}/network/custom`);\n\t\t\t},\n\t\t\tanalytics: (appId: string) => {\n\t\t\t\treturn Route<\"apps/network/analytics\">(\n\t\t\t\t\t`apps/${appId}/network/analytics`,\n\t\t\t\t);\n\t\t\t},\n\t\t},\n\t},\n} satisfies IRoutes;\n","import type { APIDeployment, DeploymentState } from \"@squarecloud/api-types/v2\";\n\nimport type { BaseApplication } from \"./application/base\";\n\n/**\n * Represents an application deployment\n */\nexport class Deployment {\n\t/** The ID of the deploy. */\n\tpublic readonly id: `git-${string}`;\n\n\t/** The current state of the deploy. */\n\tpublic state: DeploymentState;\n\n\t/** The date the deploy was created. */\n\tpublic createdAt: Date;\n\n\t/** The date the deploy was created in millisseconds. */\n\tpublic createdTimestamp: number;\n\n\t/**\n\t * Represents an application deployment\n\t *\n\t * @constructor\n\t * @param application - The application from which you fetched the deployment\n\t * @param data - The data from this deployment\n\t */\n\tconstructor(\n\t\tpublic readonly application: BaseApplication,\n\t\tdata: APIDeployment,\n\t) {\n\t\tconst { id, state, date } = data;\n\n\t\tthis.id = id;\n\t\tthis.state = state;\n\t\tthis.createdAt = new Date(date);\n\t\tthis.createdTimestamp = this.createdAt.getTime();\n\t}\n}\n","export class SquareCloudAPIError extends TypeError {\n\tconstructor(\n\t\tcode: string,\n\t\tmessage?: string,\n\t\toptions?: { stack?: string; cause?: unknown },\n\t) {\n\t\tsuper(code);\n\n\t\tthis.name = \"SquareCloudAPIError\";\n\n\t\tthis.message =\n\t\t\t(code\n\t\t\t\t?.replaceAll(\"_\", \" \")\n\t\t\t\t.toLowerCase()\n\t\t\t\t.replace(/(^|\\s)\\S/g, (L) => L.toUpperCase()) || \"UNKNOWN_CODE\") +\n\t\t\t(message ? `: ${message}` : \"\");\n\n\t\tif (options?.stack) {\n\t\t\tthis.stack = options.stack;\n\t\t}\n\n\t\tif (options?.cause) {\n\t\t\tthis.cause = options.cause;\n\t\t}\n\t}\n}\n","import { SquareCloudAPIError } from \"../structures\";\n\nexport interface AssertionProps {\n\tvalidate: (value: unknown) => boolean;\n\texpect: string;\n\tvalue: unknown;\n\tname?: string;\n}\n\nexport function assert({ validate, value, expect, name }: AssertionProps) {\n\tif (!validate(value)) {\n\t\tconst code = name ? `INVALID_${name}` : \"VALIDATION_ERROR\";\n\t\tconst message = `Expected ${expect}, got ${typeof value}`;\n\n\t\tthrow new SquareCloudAPIError(code, message);\n\t}\n}\n\nexport function makeAssertion(\n\texpect: string,\n\tvalidate: (value: unknown) => boolean,\n) {\n\treturn (value: unknown, name?: string) => {\n\t\tassert({ validate, value, expect, name });\n\t};\n}\n","import { makeAssertion } from \"./common\";\n\nexport const assertString = makeAssertion(\n\t\"string\",\n\t(value) => typeof value === \"string\",\n);\n\nexport const assertBoolean = makeAssertion(\n\t\"boolean\",\n\t(value) => typeof value === \"boolean\",\n);\n\nexport const assertPathLike = makeAssertion(\n\t\"string or Buffer\",\n\t(value) => typeof value === \"string\" || value instanceof Buffer,\n);\n"],"mappings":";;;;;;;;;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