@pxlabz/tasq-sdk 0.1.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.
- package/LICENSE +21 -0
- package/README.md +41 -0
- package/dist/index.d.ts +2152 -0
- package/dist/index.js +261 -0
- package/dist/index.js.map +1 -0
- package/package.json +56 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
// src/prefixes.ts
|
|
2
|
+
var AGENT_KEY_PREFIX = "tq_ag_";
|
|
3
|
+
var USER_TOKEN_PREFIX = "tq_us_";
|
|
4
|
+
|
|
5
|
+
// src/errors.ts
|
|
6
|
+
var TasqApiError = class extends Error {
|
|
7
|
+
code;
|
|
8
|
+
status;
|
|
9
|
+
details;
|
|
10
|
+
constructor(status, body) {
|
|
11
|
+
super(body.message);
|
|
12
|
+
this.name = "TasqApiError";
|
|
13
|
+
this.code = body.code;
|
|
14
|
+
this.status = status;
|
|
15
|
+
this.details = body.details;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
var TasqRateLimitError = class extends TasqApiError {
|
|
19
|
+
retryAfterSeconds;
|
|
20
|
+
resetAt;
|
|
21
|
+
limit;
|
|
22
|
+
remaining;
|
|
23
|
+
constructor(status, body, retryAfterHeader) {
|
|
24
|
+
super(status, body);
|
|
25
|
+
this.name = "TasqRateLimitError";
|
|
26
|
+
const details = body.details ?? {};
|
|
27
|
+
this.limit = details.limit;
|
|
28
|
+
this.remaining = details.remaining;
|
|
29
|
+
this.resetAt = details.resetAt === void 0 ? void 0 : new Date(details.resetAt);
|
|
30
|
+
const fromHeader = Number(retryAfterHeader);
|
|
31
|
+
if (retryAfterHeader !== null && Number.isFinite(fromHeader)) {
|
|
32
|
+
this.retryAfterSeconds = Math.max(0, fromHeader);
|
|
33
|
+
} else if (this.resetAt !== void 0) {
|
|
34
|
+
const ms = this.resetAt.getTime() - Date.now();
|
|
35
|
+
this.retryAfterSeconds = Math.max(0, Math.ceil(ms / 1e3));
|
|
36
|
+
} else {
|
|
37
|
+
this.retryAfterSeconds = 0;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
var TasqValidationError = class extends TasqApiError {
|
|
42
|
+
issues;
|
|
43
|
+
constructor(status, body) {
|
|
44
|
+
super(status, body);
|
|
45
|
+
this.name = "TasqValidationError";
|
|
46
|
+
this.issues = Array.isArray(body.details) ? body.details : [];
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var TasqPolicyDeniedError = class extends TasqApiError {
|
|
50
|
+
/** The rejected action name, when the API reported one. */
|
|
51
|
+
action;
|
|
52
|
+
/** The policy version quoted in the message, when the API quoted one. */
|
|
53
|
+
policyVersion;
|
|
54
|
+
constructor(status, body) {
|
|
55
|
+
super(status, body);
|
|
56
|
+
this.name = "TasqPolicyDeniedError";
|
|
57
|
+
this.action = actionFromDetails(body.details);
|
|
58
|
+
this.policyVersion = policyVersionFromMessage(body.message);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
function actionFromDetails(details) {
|
|
62
|
+
if (!Array.isArray(details)) return void 0;
|
|
63
|
+
const issues = details;
|
|
64
|
+
for (const issue of issues) {
|
|
65
|
+
if (issue.path[0] === "action") return issue.message;
|
|
66
|
+
}
|
|
67
|
+
return void 0;
|
|
68
|
+
}
|
|
69
|
+
function policyVersionFromMessage(message) {
|
|
70
|
+
const match = /policy v(?:ersion )?(\d+)/i.exec(message);
|
|
71
|
+
if (!match?.[1]) return void 0;
|
|
72
|
+
return Number(match[1]);
|
|
73
|
+
}
|
|
74
|
+
function noCredentialError() {
|
|
75
|
+
return new TasqApiError(0, {
|
|
76
|
+
code: "NO_CREDENTIAL",
|
|
77
|
+
message: "No credential configured. Run `tasq auth login`, or set TASQ_API_KEY."
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
function toTasqError(status, body, retryAfterHeader) {
|
|
81
|
+
if (status === 429 || body.code === "RATE_LIMITED") {
|
|
82
|
+
return new TasqRateLimitError(status, body, retryAfterHeader);
|
|
83
|
+
}
|
|
84
|
+
if (body.code === "VALIDATION_ERROR") {
|
|
85
|
+
return new TasqValidationError(status, body);
|
|
86
|
+
}
|
|
87
|
+
if (body.code === "AGENT_ACTION_DENIED") {
|
|
88
|
+
return new TasqPolicyDeniedError(status, body);
|
|
89
|
+
}
|
|
90
|
+
return new TasqApiError(status, body);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// src/client.ts
|
|
94
|
+
function assertPrefix(token, prefix, label) {
|
|
95
|
+
if (!token.startsWith(prefix)) {
|
|
96
|
+
throw new Error(
|
|
97
|
+
`Invalid ${label}: expected a token starting with "${prefix}".`
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function credential(auth) {
|
|
102
|
+
if (auth === void 0) return void 0;
|
|
103
|
+
if ("agentKey" in auth) {
|
|
104
|
+
assertPrefix(auth.agentKey, AGENT_KEY_PREFIX, "agent key");
|
|
105
|
+
return auth.agentKey;
|
|
106
|
+
}
|
|
107
|
+
assertPrefix(auth.userToken, USER_TOKEN_PREFIX, "user token");
|
|
108
|
+
return auth.userToken;
|
|
109
|
+
}
|
|
110
|
+
function normaliseBaseUrl(baseUrl) {
|
|
111
|
+
return `${baseUrl.replace(/\/+$/, "")}/api`;
|
|
112
|
+
}
|
|
113
|
+
function queryString(params) {
|
|
114
|
+
if (!params) return "";
|
|
115
|
+
const search = new URLSearchParams();
|
|
116
|
+
for (const [key, value] of Object.entries(params)) {
|
|
117
|
+
if (value === void 0) continue;
|
|
118
|
+
search.set(key, String(value));
|
|
119
|
+
}
|
|
120
|
+
const encoded = search.toString();
|
|
121
|
+
return encoded === "" ? "" : `?${encoded}`;
|
|
122
|
+
}
|
|
123
|
+
var TasqClient = class _TasqClient {
|
|
124
|
+
#baseUrl;
|
|
125
|
+
#token;
|
|
126
|
+
#fetch;
|
|
127
|
+
/** A client with no credential, for the two anonymous auth calls. */
|
|
128
|
+
static anonymous(baseUrl, fetchImpl) {
|
|
129
|
+
return new _TasqClient({ baseUrl, fetch: fetchImpl });
|
|
130
|
+
}
|
|
131
|
+
constructor(options) {
|
|
132
|
+
this.#baseUrl = normaliseBaseUrl(options.baseUrl);
|
|
133
|
+
this.#token = credential(options.auth);
|
|
134
|
+
this.#fetch = options.fetch ?? globalThis.fetch;
|
|
135
|
+
}
|
|
136
|
+
async #request(path, init = {}) {
|
|
137
|
+
const { anonymous = false, ...requestInit } = init;
|
|
138
|
+
const headers = new Headers(requestInit.headers);
|
|
139
|
+
if (!anonymous) {
|
|
140
|
+
if (this.#token === void 0) throw noCredentialError();
|
|
141
|
+
headers.set("authorization", `Bearer ${this.#token}`);
|
|
142
|
+
}
|
|
143
|
+
if (requestInit.body !== void 0 && !headers.has("content-type")) {
|
|
144
|
+
headers.set("content-type", "application/json");
|
|
145
|
+
}
|
|
146
|
+
headers.set("accept", "application/json");
|
|
147
|
+
const response = await this.#fetch(`${this.#baseUrl}${path}`, {
|
|
148
|
+
...requestInit,
|
|
149
|
+
headers
|
|
150
|
+
});
|
|
151
|
+
if (!response.ok) {
|
|
152
|
+
throw await this.#toError(response);
|
|
153
|
+
}
|
|
154
|
+
return await response.json();
|
|
155
|
+
}
|
|
156
|
+
async #toError(response) {
|
|
157
|
+
const retryAfter = response.headers.get("retry-after");
|
|
158
|
+
const fallback = {
|
|
159
|
+
code: `HTTP_${response.status}`,
|
|
160
|
+
message: `Request failed with status ${response.status}`
|
|
161
|
+
};
|
|
162
|
+
const envelope = await response.json().catch(() => null);
|
|
163
|
+
const error = envelope?.error;
|
|
164
|
+
const body = error?.code === void 0 ? fallback : {
|
|
165
|
+
code: error.code,
|
|
166
|
+
message: error.message ?? fallback.message,
|
|
167
|
+
details: error.details
|
|
168
|
+
};
|
|
169
|
+
return toTasqError(response.status, body, retryAfter);
|
|
170
|
+
}
|
|
171
|
+
#get(path, params) {
|
|
172
|
+
return this.#request(`${path}${queryString(params)}`);
|
|
173
|
+
}
|
|
174
|
+
workspaces = {
|
|
175
|
+
list: async () => (await this.#get("/workspaces")).workspaces,
|
|
176
|
+
get: async (slug) => (await this.#get(
|
|
177
|
+
`/workspaces/${encodeURIComponent(slug)}`
|
|
178
|
+
)).workspace
|
|
179
|
+
};
|
|
180
|
+
tasks = {
|
|
181
|
+
list: async (slug, filters) => (await this.#get(
|
|
182
|
+
`/workspaces/${encodeURIComponent(slug)}/tasks`,
|
|
183
|
+
filters
|
|
184
|
+
)).tasks,
|
|
185
|
+
get: async (slug, taskId) => (await this.#get(
|
|
186
|
+
`/workspaces/${encodeURIComponent(slug)}/tasks/${encodeURIComponent(taskId)}`
|
|
187
|
+
)).task
|
|
188
|
+
};
|
|
189
|
+
projects = {
|
|
190
|
+
list: async (slug) => (await this.#get(
|
|
191
|
+
`/workspaces/${encodeURIComponent(slug)}/projects`
|
|
192
|
+
)).projects,
|
|
193
|
+
get: async (slug, projectId) => (await this.#get(
|
|
194
|
+
`/workspaces/${encodeURIComponent(slug)}/projects/${encodeURIComponent(projectId)}`
|
|
195
|
+
)).project
|
|
196
|
+
};
|
|
197
|
+
cycles = {
|
|
198
|
+
list: async (slug, filters) => (await this.#get(
|
|
199
|
+
`/workspaces/${encodeURIComponent(slug)}/cycles`,
|
|
200
|
+
filters
|
|
201
|
+
)).cycles
|
|
202
|
+
};
|
|
203
|
+
teams = {
|
|
204
|
+
list: async (slug) => (await this.#get(
|
|
205
|
+
`/workspaces/${encodeURIComponent(slug)}/teams`
|
|
206
|
+
)).teams,
|
|
207
|
+
/** The only read that carries a team's statuses. */
|
|
208
|
+
get: async (slug, identifier) => (await this.#get(
|
|
209
|
+
`/workspaces/${encodeURIComponent(slug)}/teams/${encodeURIComponent(identifier)}`
|
|
210
|
+
)).team
|
|
211
|
+
};
|
|
212
|
+
labels = {
|
|
213
|
+
list: async (slug) => (await this.#get(
|
|
214
|
+
`/workspaces/${encodeURIComponent(slug)}/labels`
|
|
215
|
+
)).labels
|
|
216
|
+
};
|
|
217
|
+
agents = {
|
|
218
|
+
policy: (slug, agentId) => this.#get(
|
|
219
|
+
`/workspaces/${encodeURIComponent(slug)}/agents/${encodeURIComponent(agentId)}/policy`
|
|
220
|
+
)
|
|
221
|
+
};
|
|
222
|
+
auth = {
|
|
223
|
+
/** The principal behind the current credential. */
|
|
224
|
+
me: async () => (await this.#get("/auth/me")).user,
|
|
225
|
+
/** Step one of CLI login: mail a sign-in code. Needs no credential. */
|
|
226
|
+
requestMagicLink: async (input) => {
|
|
227
|
+
await this.#request("/auth/magic-link", {
|
|
228
|
+
method: "POST",
|
|
229
|
+
body: JSON.stringify({ email: input.email }),
|
|
230
|
+
anonymous: true
|
|
231
|
+
});
|
|
232
|
+
},
|
|
233
|
+
/** Step two: exchange the code for a `tq_us_…` token. Needs no credential. */
|
|
234
|
+
verifyCliCode: (input) => this.#request("/auth/cli/verify-code", {
|
|
235
|
+
method: "POST",
|
|
236
|
+
body: JSON.stringify(input),
|
|
237
|
+
anonymous: true
|
|
238
|
+
}),
|
|
239
|
+
/** Revoke the user token this client authenticates with. */
|
|
240
|
+
revokeUserToken: async () => {
|
|
241
|
+
await this.#request("/auth/cli/token", { method: "DELETE" });
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
/** Run a write action. The API validates `input`; the SDK does not. */
|
|
245
|
+
dispatch(action, slug, input) {
|
|
246
|
+
return this.#request(
|
|
247
|
+
`/workspaces/${encodeURIComponent(slug)}/actions`,
|
|
248
|
+
{ method: "POST", body: JSON.stringify({ action, input }) }
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
export {
|
|
253
|
+
AGENT_KEY_PREFIX,
|
|
254
|
+
TasqApiError,
|
|
255
|
+
TasqClient,
|
|
256
|
+
TasqPolicyDeniedError,
|
|
257
|
+
TasqRateLimitError,
|
|
258
|
+
TasqValidationError,
|
|
259
|
+
USER_TOKEN_PREFIX
|
|
260
|
+
};
|
|
261
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/prefixes.ts","../src/errors.ts","../src/client.ts"],"sourcesContent":["/**\n * Token prefixes, duplicated from `@tasq/contracts/agents`.\n *\n * They are re-declared here rather than re-exported because contracts is\n * private: tsup's dts bundler leaves a `@tasq/contracts/agents` import in the\n * emitted `.d.ts`, which a published consumer cannot resolve.\n * `src/prefixes.equality.ts` asserts these stay in step with contracts.\n */\n\n/** Plaintext prefix of an agent API key. */\nexport const AGENT_KEY_PREFIX = \"tq_ag_\";\n\n/** Plaintext prefix of a user (CLI) token. */\nexport const USER_TOKEN_PREFIX = \"tq_us_\";\n","/** Shape of the API's error envelope: `{ error: { code, message, details? } }`. */\ntype ErrorBody = {\n\tcode: string;\n\tmessage: string;\n\tdetails?: unknown;\n};\n\n/** Every failure the client raises for a non-2xx response. */\nexport class TasqApiError extends Error {\n\treadonly code: string;\n\treadonly status: number;\n\treadonly details: unknown;\n\n\tconstructor(status: number, body: ErrorBody) {\n\t\tsuper(body.message);\n\t\tthis.name = \"TasqApiError\";\n\t\tthis.code = body.code;\n\t\tthis.status = status;\n\t\tthis.details = body.details;\n\t}\n}\n\ntype RateLimitDetails = {\n\tlimit?: number;\n\tremaining?: number;\n\tresetAt?: string;\n};\n\n/** A `429 RATE_LIMITED`. `retryAfterSeconds` is never negative. */\nexport class TasqRateLimitError extends TasqApiError {\n\treadonly retryAfterSeconds: number;\n\treadonly resetAt: Date | undefined;\n\treadonly limit: number | undefined;\n\treadonly remaining: number | undefined;\n\n\tconstructor(status: number, body: ErrorBody, retryAfterHeader: string | null) {\n\t\tsuper(status, body);\n\t\tthis.name = \"TasqRateLimitError\";\n\n\t\tconst details = (body.details ?? {}) as RateLimitDetails;\n\n\t\tthis.limit = details.limit;\n\t\tthis.remaining = details.remaining;\n\t\tthis.resetAt =\n\t\t\tdetails.resetAt === undefined ? undefined : new Date(details.resetAt);\n\n\t\tconst fromHeader = Number(retryAfterHeader);\n\n\t\tif (retryAfterHeader !== null && Number.isFinite(fromHeader)) {\n\t\t\tthis.retryAfterSeconds = Math.max(0, fromHeader);\n\t\t} else if (this.resetAt !== undefined) {\n\t\t\tconst ms = this.resetAt.getTime() - Date.now();\n\n\t\t\tthis.retryAfterSeconds = Math.max(0, Math.ceil(ms / 1000));\n\t\t} else {\n\t\t\tthis.retryAfterSeconds = 0;\n\t\t}\n\t}\n}\n\nexport type TasqValidationIssue = {\n\tpath: (string | number)[];\n\tmessage: string;\n};\n\n/** A `400 VALIDATION_ERROR`. `issues` mirrors the API's Zod issue list. */\nexport class TasqValidationError extends TasqApiError {\n\treadonly issues: TasqValidationIssue[];\n\n\tconstructor(status: number, body: ErrorBody) {\n\t\tsuper(status, body);\n\t\tthis.name = \"TasqValidationError\";\n\t\tthis.issues = Array.isArray(body.details)\n\t\t\t? (body.details as TasqValidationIssue[])\n\t\t\t: [];\n\t}\n}\n\n/** A `403 AGENT_ACTION_DENIED`: the agent's policy does not allow the action. */\nexport class TasqPolicyDeniedError extends TasqApiError {\n\t/** The rejected action name, when the API reported one. */\n\treadonly action: string | undefined;\n\t/** The policy version quoted in the message, when the API quoted one. */\n\treadonly policyVersion: number | undefined;\n\n\tconstructor(status: number, body: ErrorBody) {\n\t\tsuper(status, body);\n\t\tthis.name = \"TasqPolicyDeniedError\";\n\t\tthis.action = actionFromDetails(body.details);\n\t\tthis.policyVersion = policyVersionFromMessage(body.message);\n\t}\n}\n\nfunction actionFromDetails(details: ErrorBody[\"details\"]): string | undefined {\n\tif (!Array.isArray(details)) return undefined;\n\n\tconst issues = details as TasqValidationIssue[];\n\n\tfor (const issue of issues) {\n\t\tif (issue.path[0] === \"action\") return issue.message;\n\t}\n\n\treturn undefined;\n}\n\nfunction policyVersionFromMessage(message: string): number | undefined {\n\tconst match = /policy v(?:ersion )?(\\d+)/i.exec(message);\n\n\tif (!match?.[1]) return undefined;\n\n\treturn Number(match[1]);\n}\n\n/**\n * Raised before any request when the client has no credential but the called\n * method needs one. Shaped like an API error so callers map it uniformly.\n */\nexport function noCredentialError(): TasqApiError {\n\treturn new TasqApiError(0, {\n\t\tcode: \"NO_CREDENTIAL\",\n\t\tmessage:\n\t\t\t\"No credential configured. Run `tasq auth login`, or set TASQ_API_KEY.\",\n\t});\n}\n\n/** Build the right error subclass for a failed response. */\nexport function toTasqError(\n\tstatus: number,\n\tbody: ErrorBody,\n\tretryAfterHeader: string | null,\n): TasqApiError {\n\tif (status === 429 || body.code === \"RATE_LIMITED\") {\n\t\treturn new TasqRateLimitError(status, body, retryAfterHeader);\n\t}\n\n\tif (body.code === \"VALIDATION_ERROR\") {\n\t\treturn new TasqValidationError(status, body);\n\t}\n\n\tif (body.code === \"AGENT_ACTION_DENIED\") {\n\t\treturn new TasqPolicyDeniedError(status, body);\n\t}\n\n\treturn new TasqApiError(status, body);\n}\n","import type {\n\tActionInputMap,\n\tActionName,\n\tDispatchResponse,\n} from \"./contracts\";\nimport { AGENT_KEY_PREFIX, USER_TOKEN_PREFIX } from \"./prefixes\";\nimport { noCredentialError, type TasqApiError, toTasqError } from \"./errors\";\nimport type {\n\tAgentPolicyDetail,\n\tCliTokenIssued,\n\tCurrentUser,\n\tCycleListFilters,\n\tCycleListItem,\n\tLabelListItem,\n\tProjectDetail,\n\tProjectListItem,\n\tTaskDetail,\n\tTaskListFilters,\n\tTaskListItem,\n\tTeamDetail,\n\tTeamListItem,\n\tWorkspaceDetail,\n\tWorkspaceSummary,\n} from \"./types\";\n\n/** Either an agent key (`tq_ag_…`) or a user token (`tq_us_…`). */\nexport type TasqAuth = { agentKey: string } | { userToken: string };\n\nexport type TasqClientOptions = {\n\t/** Origin of the API, with or without a trailing slash. `/api` is appended. */\n\tbaseUrl: string;\n\t/**\n\t * Omit to build an anonymous client. Only `auth.requestMagicLink` and\n\t * `auth.verifyCliCode` work without one; everything else throws\n\t * `NO_CREDENTIAL` before reaching the network.\n\t */\n\tauth?: TasqAuth;\n\t/** Injectable for tests and for runtimes with a non-global `fetch`. */\n\tfetch?: typeof fetch;\n};\n\ntype QueryValue = string | number | boolean | undefined;\n\nfunction assertPrefix(token: string, prefix: string, label: string): void {\n\tif (!token.startsWith(prefix)) {\n\t\tthrow new Error(\n\t\t\t`Invalid ${label}: expected a token starting with \"${prefix}\".`,\n\t\t);\n\t}\n}\n\nfunction credential(auth: TasqAuth | undefined): string | undefined {\n\tif (auth === undefined) return undefined;\n\n\tif (\"agentKey\" in auth) {\n\t\tassertPrefix(auth.agentKey, AGENT_KEY_PREFIX, \"agent key\");\n\n\t\treturn auth.agentKey;\n\t}\n\n\tassertPrefix(auth.userToken, USER_TOKEN_PREFIX, \"user token\");\n\n\treturn auth.userToken;\n}\n\nfunction normaliseBaseUrl(baseUrl: string): string {\n\treturn `${baseUrl.replace(/\\/+$/, \"\")}/api`;\n}\n\nfunction queryString(params: Record<string, QueryValue> | undefined): string {\n\tif (!params) return \"\";\n\n\tconst search = new URLSearchParams();\n\n\tfor (const [key, value] of Object.entries(params)) {\n\t\tif (value === undefined) continue;\n\n\t\tsearch.set(key, String(value));\n\t}\n\n\tconst encoded = search.toString();\n\n\treturn encoded === \"\" ? \"\" : `?${encoded}`;\n}\n\nexport class TasqClient {\n\treadonly #baseUrl: string;\n\treadonly #token: string | undefined;\n\treadonly #fetch: typeof fetch;\n\n\t/** A client with no credential, for the two anonymous auth calls. */\n\tstatic anonymous(baseUrl: string, fetchImpl?: typeof fetch): TasqClient {\n\t\treturn new TasqClient({ baseUrl, fetch: fetchImpl });\n\t}\n\n\tconstructor(options: TasqClientOptions) {\n\t\tthis.#baseUrl = normaliseBaseUrl(options.baseUrl);\n\t\tthis.#token = credential(options.auth);\n\t\tthis.#fetch = options.fetch ?? globalThis.fetch;\n\t}\n\n\tasync #request<T>(\n\t\tpath: string,\n\t\tinit: RequestInit & { anonymous?: boolean } = {},\n\t): Promise<T> {\n\t\tconst { anonymous = false, ...requestInit } = init;\n\t\tconst headers = new Headers(requestInit.headers);\n\n\t\tif (!anonymous) {\n\t\t\tif (this.#token === undefined) throw noCredentialError();\n\n\t\t\theaders.set(\"authorization\", `Bearer ${this.#token}`);\n\t\t}\n\n\t\tif (requestInit.body !== undefined && !headers.has(\"content-type\")) {\n\t\t\theaders.set(\"content-type\", \"application/json\");\n\t\t}\n\n\t\theaders.set(\"accept\", \"application/json\");\n\n\t\tconst response = await this.#fetch(`${this.#baseUrl}${path}`, {\n\t\t\t...requestInit,\n\t\t\theaders,\n\t\t});\n\n\t\tif (!response.ok) {\n\t\t\tthrow await this.#toError(response);\n\t\t}\n\n\t\treturn (await response.json()) as T;\n\t}\n\n\tasync #toError(response: Response): Promise<TasqApiError> {\n\t\tconst retryAfter = response.headers.get(\"retry-after\");\n\n\t\tconst fallback = {\n\t\t\tcode: `HTTP_${response.status}`,\n\t\t\tmessage: `Request failed with status ${response.status}`,\n\t\t};\n\n\t\tconst envelope = (await response.json().catch(() => null)) as {\n\t\t\terror?: { code?: string; message?: string; details?: unknown };\n\t\t} | null;\n\n\t\tconst error = envelope?.error;\n\n\t\tconst body =\n\t\t\terror?.code === undefined\n\t\t\t\t? fallback\n\t\t\t\t: {\n\t\t\t\t\t\tcode: error.code,\n\t\t\t\t\t\tmessage: error.message ?? fallback.message,\n\t\t\t\t\t\tdetails: error.details,\n\t\t\t\t\t};\n\n\t\treturn toTasqError(response.status, body, retryAfter);\n\t}\n\n\t#get<T>(path: string, params?: Record<string, QueryValue>): Promise<T> {\n\t\treturn this.#request<T>(`${path}${queryString(params)}`);\n\t}\n\n\treadonly workspaces = {\n\t\tlist: async (): Promise<WorkspaceSummary[]> =>\n\t\t\t(await this.#get<{ workspaces: WorkspaceSummary[] }>(\"/workspaces\"))\n\t\t\t\t.workspaces,\n\n\t\tget: async (slug: string): Promise<WorkspaceDetail> =>\n\t\t\t(\n\t\t\t\tawait this.#get<{ workspace: WorkspaceDetail }>(\n\t\t\t\t\t`/workspaces/${encodeURIComponent(slug)}`,\n\t\t\t\t)\n\t\t\t).workspace,\n\t};\n\n\treadonly tasks = {\n\t\tlist: async (\n\t\t\tslug: string,\n\t\t\tfilters?: TaskListFilters,\n\t\t): Promise<TaskListItem[]> =>\n\t\t\t(\n\t\t\t\tawait this.#get<{ tasks: TaskListItem[] }>(\n\t\t\t\t\t`/workspaces/${encodeURIComponent(slug)}/tasks`,\n\t\t\t\t\tfilters,\n\t\t\t\t)\n\t\t\t).tasks,\n\n\t\tget: async (slug: string, taskId: string): Promise<TaskDetail> =>\n\t\t\t(\n\t\t\t\tawait this.#get<{ task: TaskDetail }>(\n\t\t\t\t\t`/workspaces/${encodeURIComponent(slug)}/tasks/${encodeURIComponent(taskId)}`,\n\t\t\t\t)\n\t\t\t).task,\n\t};\n\n\treadonly projects = {\n\t\tlist: async (slug: string): Promise<ProjectListItem[]> =>\n\t\t\t(\n\t\t\t\tawait this.#get<{ projects: ProjectListItem[] }>(\n\t\t\t\t\t`/workspaces/${encodeURIComponent(slug)}/projects`,\n\t\t\t\t)\n\t\t\t).projects,\n\n\t\tget: async (slug: string, projectId: string): Promise<ProjectDetail> =>\n\t\t\t(\n\t\t\t\tawait this.#get<{ project: ProjectDetail }>(\n\t\t\t\t\t`/workspaces/${encodeURIComponent(slug)}/projects/${encodeURIComponent(projectId)}`,\n\t\t\t\t)\n\t\t\t).project,\n\t};\n\n\treadonly cycles = {\n\t\tlist: async (\n\t\t\tslug: string,\n\t\t\tfilters?: CycleListFilters,\n\t\t): Promise<CycleListItem[]> =>\n\t\t\t(\n\t\t\t\tawait this.#get<{ cycles: CycleListItem[] }>(\n\t\t\t\t\t`/workspaces/${encodeURIComponent(slug)}/cycles`,\n\t\t\t\t\tfilters,\n\t\t\t\t)\n\t\t\t).cycles,\n\n\t};\n\n\treadonly teams = {\n\t\tlist: async (slug: string): Promise<TeamListItem[]> =>\n\t\t\t(\n\t\t\t\tawait this.#get<{ teams: TeamListItem[] }>(\n\t\t\t\t\t`/workspaces/${encodeURIComponent(slug)}/teams`,\n\t\t\t\t)\n\t\t\t).teams,\n\n\t\t/** The only read that carries a team's statuses. */\n\t\tget: async (slug: string, identifier: string): Promise<TeamDetail> =>\n\t\t\t(\n\t\t\t\tawait this.#get<{ team: TeamDetail }>(\n\t\t\t\t\t`/workspaces/${encodeURIComponent(slug)}/teams/${encodeURIComponent(identifier)}`,\n\t\t\t\t)\n\t\t\t).team,\n\t};\n\n\treadonly labels = {\n\t\tlist: async (slug: string): Promise<LabelListItem[]> =>\n\t\t\t(\n\t\t\t\tawait this.#get<{ labels: LabelListItem[] }>(\n\t\t\t\t\t`/workspaces/${encodeURIComponent(slug)}/labels`,\n\t\t\t\t)\n\t\t\t).labels,\n\t};\n\n\treadonly agents = {\n\t\tpolicy: (slug: string, agentId: string): Promise<AgentPolicyDetail> =>\n\t\t\tthis.#get<AgentPolicyDetail>(\n\t\t\t\t`/workspaces/${encodeURIComponent(slug)}/agents/${encodeURIComponent(agentId)}/policy`,\n\t\t\t),\n\t};\n\n\treadonly auth = {\n\t\t/** The principal behind the current credential. */\n\t\tme: async (): Promise<CurrentUser> =>\n\t\t\t(await this.#get<{ user: CurrentUser }>(\"/auth/me\")).user,\n\n\t\t/** Step one of CLI login: mail a sign-in code. Needs no credential. */\n\t\trequestMagicLink: async (input: { email: string }): Promise<void> => {\n\t\t\tawait this.#request<unknown>(\"/auth/magic-link\", {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\tbody: JSON.stringify({ email: input.email }),\n\t\t\t\tanonymous: true,\n\t\t\t});\n\t\t},\n\n\t\t/** Step two: exchange the code for a `tq_us_…` token. Needs no credential. */\n\t\tverifyCliCode: (input: {\n\t\t\temail: string;\n\t\t\tcode: string;\n\t\t\tlabel: string;\n\t\t}): Promise<CliTokenIssued> =>\n\t\t\tthis.#request<CliTokenIssued>(\"/auth/cli/verify-code\", {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\tbody: JSON.stringify(input),\n\t\t\t\tanonymous: true,\n\t\t\t}),\n\n\t\t/** Revoke the user token this client authenticates with. */\n\t\trevokeUserToken: async (): Promise<void> => {\n\t\t\tawait this.#request<unknown>(\"/auth/cli/token\", { method: \"DELETE\" });\n\t\t},\n\t};\n\n\t/** Run a write action. The API validates `input`; the SDK does not. */\n\tdispatch<TName extends ActionName>(\n\t\taction: TName,\n\t\tslug: string,\n\t\tinput: ActionInputMap[TName],\n\t): Promise<DispatchResponse<TName>> {\n\t\treturn this.#request<DispatchResponse<TName>>(\n\t\t\t`/workspaces/${encodeURIComponent(slug)}/actions`,\n\t\t\t{ method: \"POST\", body: JSON.stringify({ action, input }) },\n\t\t);\n\t}\n}\n"],"mappings":";AAUO,IAAM,mBAAmB;AAGzB,IAAM,oBAAoB;;;ACL1B,IAAM,eAAN,cAA2B,MAAM;AAAA,EAC9B;AAAA,EACA;AAAA,EACA;AAAA,EAET,YAAY,QAAgB,MAAiB;AAC5C,UAAM,KAAK,OAAO;AAClB,SAAK,OAAO;AACZ,SAAK,OAAO,KAAK;AACjB,SAAK,SAAS;AACd,SAAK,UAAU,KAAK;AAAA,EACrB;AACD;AASO,IAAM,qBAAN,cAAiC,aAAa;AAAA,EAC3C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAET,YAAY,QAAgB,MAAiB,kBAAiC;AAC7E,UAAM,QAAQ,IAAI;AAClB,SAAK,OAAO;AAEZ,UAAM,UAAW,KAAK,WAAW,CAAC;AAElC,SAAK,QAAQ,QAAQ;AACrB,SAAK,YAAY,QAAQ;AACzB,SAAK,UACJ,QAAQ,YAAY,SAAY,SAAY,IAAI,KAAK,QAAQ,OAAO;AAErE,UAAM,aAAa,OAAO,gBAAgB;AAE1C,QAAI,qBAAqB,QAAQ,OAAO,SAAS,UAAU,GAAG;AAC7D,WAAK,oBAAoB,KAAK,IAAI,GAAG,UAAU;AAAA,IAChD,WAAW,KAAK,YAAY,QAAW;AACtC,YAAM,KAAK,KAAK,QAAQ,QAAQ,IAAI,KAAK,IAAI;AAE7C,WAAK,oBAAoB,KAAK,IAAI,GAAG,KAAK,KAAK,KAAK,GAAI,CAAC;AAAA,IAC1D,OAAO;AACN,WAAK,o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AAK;AAAA,MACJ,eAAe,mBAAmB,IAAI,CAAC,WAAW,mBAAmB,OAAO,CAAC;AAAA,IAC9E;AAAA,EACF;AAAA,EAES,OAAO;AAAA;AAAA,IAEf,IAAI,aACF,MAAM,KAAK,KAA4B,UAAU,GAAG;AAAA;AAAA,IAGtD,kBAAkB,OAAO,UAA4C;AACpE,YAAM,KAAK,SAAkB,oBAAoB;AAAA,QAChD,QAAQ;AAAA,QACR,MAAM,KAAK,UAAU,EAAE,OAAO,MAAM,MAAM,CAAC;AAAA,QAC3C,WAAW;AAAA,MACZ,CAAC;AAAA,IACF;AAAA;AAAA,IAGA,eAAe,CAAC,UAKf,KAAK,SAAyB,yBAAyB;AAAA,MACtD,QAAQ;AAAA,MACR,MAAM,KAAK,UAAU,KAAK;AAAA,MAC1B,WAAW;AAAA,IACZ,CAAC;AAAA;AAAA,IAGF,iBAAiB,YAA2B;AAC3C,YAAM,KAAK,SAAkB,mBAAmB,EAAE,QAAQ,SAAS,CAAC;AAAA,IACrE;AAAA,EACD;AAAA;AAAA,EAGA,SACC,QACA,MACA,OACmC;AACnC,WAAO,KAAK;AAAA,MACX,eAAe,mBAAmB,IAAI,CAAC;AAAA,MACvC,EAAE,QAAQ,QAAQ,MAAM,KAAK,UAAU,EAAE,QAAQ,MAAM,CAAC,EAAE;AAAA,IAC3D;AAAA,EACD;AACD;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pxlabz/tasq-sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Typed client for the Tasq API: tasks, projects, cycles, and agent actions",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"tasq",
|
|
7
|
+
"sdk",
|
|
8
|
+
"agents",
|
|
9
|
+
"project-management",
|
|
10
|
+
"api-client"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"author": "Paul Cedrick Artigo",
|
|
14
|
+
"homepage": "https://github.com/pxlabz/tasq/tree/main/packages/sdk#readme",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/pxlabz/tasq.git",
|
|
18
|
+
"directory": "packages/sdk"
|
|
19
|
+
},
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/pxlabz/tasq/issues"
|
|
22
|
+
},
|
|
23
|
+
"private": false,
|
|
24
|
+
"type": "module",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"import": "./dist/index.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist",
|
|
33
|
+
"README.md",
|
|
34
|
+
"LICENSE"
|
|
35
|
+
],
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"sideEffects": false,
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=20"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "tsup",
|
|
45
|
+
"generate:contracts": "bun scripts/generate-contract-types.ts",
|
|
46
|
+
"typecheck": "tsc --project tsconfig.json --noEmit",
|
|
47
|
+
"lint": "cd ../.. && bun --bun ./node_modules/oxlint/bin/oxlint packages/sdk",
|
|
48
|
+
"test": "bun test"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@types/bun": "^1.3.12",
|
|
52
|
+
"hono": "^4.12.12",
|
|
53
|
+
"tsup": "^8.5.0",
|
|
54
|
+
"typescript": "^5.7.2"
|
|
55
|
+
}
|
|
56
|
+
}
|