quickbase-js 0.1.0-beta.7 → 0.1.0-beta.9
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/CHANGELOG.md +55 -0
- package/README.md +46 -130
- package/dist/esm/quickbase.d.ts +1292 -125
- package/dist/esm/quickbase.js +666 -190
- package/dist/esm/quickbase.js.map +1 -1
- package/dist/esm/quickbase.min.js +1 -1
- package/dist/esm/quickbase.min.js.map +1 -1
- package/dist/umd/quickbase.umd.js +674 -189
- package/dist/umd/quickbase.umd.js.map +1 -1
- package/dist/umd/quickbase.umd.min.js +1 -1
- package/dist/umd/quickbase.umd.min.js.map +1 -1
- package/open-api/fix-spec-main.ts +69 -68
- package/open-api/generate-override-report.ts +214 -0
- package/open-api/generate-unified-interface.ts +162 -103
- package/open-api/output/override-report.html +11 -0
- package/open-api/output/override-report.json +14395 -58
- package/open-api/output/quickbase-fixed.json +583 -2
- package/open-api/schema/tags/fields.ts +331 -16
- package/open-api/schema/tags/index.ts +2 -4
- package/open-api/types/spec.ts +2 -1
- package/open-api/utils/generateJsDoc.ts +96 -0
- package/package.json +33 -33
- package/src/RateLimitError.ts +12 -0
- package/src/ThrottleBucket.ts +41 -0
- package/src/authorizationStrategy.ts +246 -0
- package/src/generated-unified/QuickbaseClient.ts +809 -119
- package/src/invokeMethod.ts +242 -0
- package/src/quickbaseClient.ts +117 -248
- package/src/rateLimiter.ts +25 -0
- package/src/tokenCache.ts +10 -26
- package/tests/setup.ts +2 -0
- package/tests/vitest/qb/apps/getApp.test.ts +6 -2
- package/tests/vitest/qb/apps/getAppEvents.test.ts +67 -0
- package/tests/vitest/qb/apps/updateApp.test.ts +58 -0
- package/tests/vitest/qb/auth/mockQuickbaseServer.ts +60 -0
- package/tests/vitest/qb/auth/retryUserToken401.test.ts +73 -0
- package/tests/vitest/qb/auth/ssoTokenIntegrationWithServer.test.ts +65 -0
- package/tests/vitest/qb/fields/createField.test.ts +1 -2
- package/tests/vitest/qb/fields/deleteFields.test.ts +10 -16
- package/tests/vitest/qb/fields/getFieldUsage.test.ts +58 -150
- package/tests/vitest/qb/fields/updateField.test.ts +93 -0
- package/tests/vitest/qb/formulas/runFormula.test.ts +96 -0
- package/tests/vitest/qb/index.ts +1 -0
- package/tests/vitest/qb/tables/createRelationship.test.ts +102 -0
- package/tests/vitest/qb/tables/deleteRelationship.test.ts +143 -0
- package/tests/vitest/qb/tables/updateRelationship.test.ts +250 -0
- package/tests/vitest/unit/ThrottleBucket.test.ts +95 -0
- package/tests/vitest/unit/apps/copyApp.test.ts +133 -40
- package/tests/vitest/unit/apps/getAppEvents.test.ts +193 -0
- package/tests/vitest/unit/apps/updateApp.test.ts +412 -0
- package/tests/vitest/unit/auth/fetchTempToken401.test.ts +7 -26
- package/tests/vitest/unit/auth/getTempToken.test.ts +42 -18
- package/tests/vitest/unit/auth/retryTempToken401.test.ts +95 -0
- package/tests/vitest/unit/auth/retryTempTokenMaxRetries.test.ts +235 -0
- package/tests/vitest/unit/auth/retryUserToken401.test.ts +69 -0
- package/tests/vitest/unit/auth/ssoTokenRefresh.test.ts +325 -0
- package/tests/vitest/unit/fields/createField.test.ts +180 -120
- package/tests/vitest/unit/fields/getFieldUsage.test.ts +47 -14
- package/tests/vitest/unit/fields/updateField.test.ts +285 -0
- package/tests/vitest/unit/formulas/runFormula.test.ts +205 -0
- package/tests/vitest/unit/index.ts +2 -1
- package/tests/vitest/unit/inferHttpMethod.test.ts +131 -0
- package/tests/vitest/unit/rateLimit.test.ts +191 -0
- package/tests/vitest/unit/tables/createRelationship.test.ts +212 -0
- package/tests/vitest/unit/tables/deleteRelationship.test.ts +97 -0
- package/tests/vitest/unit/tables/updateRelationship.test.ts +307 -0
- package/tests/vitest/unit/tokenLifespan.test.ts +204 -0
- package/tools/log-gen.ts +20 -7
- package/tools/log-gen.yaml +3446 -1848
- package/tsconfig.json +5 -2
- package/vitest.config.ts +2 -2
- package/notes.txt +0 -158
- package/tests/vitest/unit/auth/retryOn401.test.ts +0 -116
- package/tests/vitest/unit/auth/two401s.test.ts +0 -86
- package/tools/generate-types.ts +0 -29
- package/tools/openapi-schema-validator.ts +0 -38
- package/tools/spec-analyzer.ts +0 -163
- package/tools/split-spec.ts +0 -148
- package/tools/test-import.ts +0 -3
- package/tools/validate-spec.ts +0 -40
- /package/tests/playwright/qb/auth/{tokenPrefetch.test.ts → tempTokenPrefetch.test.ts} +0 -0
- /package/tests/playwright/qb/auth/{tokenRenewal.test.ts → tempTokenRenewal.test.ts} +0 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
// tests/vitest/unit/rateLimit.test.ts
|
|
2
|
+
import { describe, it, expect, beforeEach } from "vitest";
|
|
3
|
+
import {
|
|
4
|
+
createClient,
|
|
5
|
+
mockFetch,
|
|
6
|
+
QB_APP_ID,
|
|
7
|
+
QB_REALM,
|
|
8
|
+
QB_USER_TOKEN,
|
|
9
|
+
} from "@tests/setup.ts";
|
|
10
|
+
import { RateLimitError } from "@/RateLimitError";
|
|
11
|
+
import { GetApp200Response } from "@/generated/models";
|
|
12
|
+
|
|
13
|
+
describe("QuickbaseClient Unit - Rate Limit Handling", () => {
|
|
14
|
+
let client: ReturnType<typeof createClient>;
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
mockFetch.mockClear();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("succeeds after retrying on 429 Too Many Requests", async () => {
|
|
21
|
+
client = createClient(mockFetch, {
|
|
22
|
+
realm: QB_REALM,
|
|
23
|
+
userToken: QB_USER_TOKEN,
|
|
24
|
+
debug: true,
|
|
25
|
+
maxRetries: 2,
|
|
26
|
+
retryDelay: 100,
|
|
27
|
+
throttle: { rate: 10, burst: 10 },
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const appId = QB_APP_ID;
|
|
31
|
+
|
|
32
|
+
const mockResponse: GetApp200Response = {
|
|
33
|
+
id: appId,
|
|
34
|
+
name: "TestApp",
|
|
35
|
+
created: "2025-01-01T00:00:00Z",
|
|
36
|
+
updated: "2025-01-02T00:00:00Z",
|
|
37
|
+
description: "Test app",
|
|
38
|
+
timeZone: "(UTC-08:00) Pacific Time (US & Canada)",
|
|
39
|
+
dateFormat: "MM-DD-YYYY",
|
|
40
|
+
hasEveryoneOnTheInternet: false,
|
|
41
|
+
memoryInfo: { estMemory: 0, estMemoryInclDependentApps: 0 },
|
|
42
|
+
securityProperties: {
|
|
43
|
+
allowClone: false,
|
|
44
|
+
allowExport: true,
|
|
45
|
+
enableAppTokens: true,
|
|
46
|
+
hideFromPublic: false,
|
|
47
|
+
mustBeRealmApproved: false,
|
|
48
|
+
useIPFilter: false,
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
mockFetch
|
|
53
|
+
.mockResolvedValueOnce({
|
|
54
|
+
ok: false,
|
|
55
|
+
status: 429,
|
|
56
|
+
headers: new Headers({ "Retry-After": "1" }),
|
|
57
|
+
json: () => Promise.resolve({ message: "Too Many Requests" }),
|
|
58
|
+
})
|
|
59
|
+
.mockResolvedValueOnce({
|
|
60
|
+
ok: true,
|
|
61
|
+
status: 200,
|
|
62
|
+
json: () => Promise.resolve(mockResponse),
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const result = await client.getApp({ appId });
|
|
66
|
+
|
|
67
|
+
expect(result).toEqual({
|
|
68
|
+
...mockResponse,
|
|
69
|
+
created: new Date("2025-01-01T00:00:00Z"),
|
|
70
|
+
updated: new Date("2025-01-02T00:00:00Z"),
|
|
71
|
+
});
|
|
72
|
+
expect(mockFetch).toHaveBeenCalledTimes(2);
|
|
73
|
+
expect(mockFetch).toHaveBeenNthCalledWith(
|
|
74
|
+
1,
|
|
75
|
+
`https://api.quickbase.com/v1/apps/${appId}`,
|
|
76
|
+
expect.objectContaining({
|
|
77
|
+
method: "GET",
|
|
78
|
+
headers: expect.objectContaining({
|
|
79
|
+
"QB-Realm-Hostname": `${QB_REALM}.quickbase.com`,
|
|
80
|
+
Authorization: `QB-USER-TOKEN ${QB_USER_TOKEN}`,
|
|
81
|
+
"Content-Type": "application/json",
|
|
82
|
+
}),
|
|
83
|
+
credentials: "omit",
|
|
84
|
+
})
|
|
85
|
+
);
|
|
86
|
+
expect(mockFetch).toHaveBeenNthCalledWith(
|
|
87
|
+
2,
|
|
88
|
+
`https://api.quickbase.com/v1/apps/${appId}`,
|
|
89
|
+
expect.objectContaining({
|
|
90
|
+
method: "GET",
|
|
91
|
+
headers: expect.objectContaining({
|
|
92
|
+
"QB-Realm-Hostname": `${QB_REALM}.quickbase.com`,
|
|
93
|
+
Authorization: `QB-USER-TOKEN ${QB_USER_TOKEN}`,
|
|
94
|
+
"Content-Type": "application/json",
|
|
95
|
+
}),
|
|
96
|
+
credentials: "omit",
|
|
97
|
+
})
|
|
98
|
+
);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("throws RateLimitError after max retries on 429", async () => {
|
|
102
|
+
client = createClient(mockFetch, {
|
|
103
|
+
realm: QB_REALM,
|
|
104
|
+
userToken: QB_USER_TOKEN,
|
|
105
|
+
debug: true,
|
|
106
|
+
maxRetries: 1,
|
|
107
|
+
retryDelay: 100,
|
|
108
|
+
throttle: { rate: 10, burst: 10 },
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
const appId = QB_APP_ID;
|
|
112
|
+
|
|
113
|
+
mockFetch
|
|
114
|
+
.mockResolvedValueOnce({
|
|
115
|
+
ok: false,
|
|
116
|
+
status: 429,
|
|
117
|
+
headers: new Headers({ "Retry-After": "1" }),
|
|
118
|
+
json: () => Promise.resolve({ message: "Too Many Requests" }),
|
|
119
|
+
})
|
|
120
|
+
.mockResolvedValueOnce({
|
|
121
|
+
ok: false,
|
|
122
|
+
status: 429,
|
|
123
|
+
headers: new Headers({ "Retry-After": "1" }),
|
|
124
|
+
json: () => Promise.resolve({ message: "Too Many Requests" }),
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
await expect(client.getApp({ appId })).rejects.toMatchObject({
|
|
128
|
+
name: "RateLimitError",
|
|
129
|
+
message: "API Error: Too Many Requests (Status: 429)",
|
|
130
|
+
status: 429,
|
|
131
|
+
retryAfter: 1,
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
expect(mockFetch).toHaveBeenCalledTimes(2);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("respects throttle limits without exceeding rate", async () => {
|
|
138
|
+
client = createClient(mockFetch, {
|
|
139
|
+
realm: QB_REALM,
|
|
140
|
+
userToken: QB_USER_TOKEN,
|
|
141
|
+
debug: true,
|
|
142
|
+
throttle: { rate: 5, burst: 5 },
|
|
143
|
+
maxRetries: 0, // Prevent retries for this test
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
const appId = QB_APP_ID;
|
|
147
|
+
|
|
148
|
+
const mockResponse: GetApp200Response = {
|
|
149
|
+
id: appId,
|
|
150
|
+
name: "TestApp",
|
|
151
|
+
created: "2025-01-01T00:00:00Z",
|
|
152
|
+
updated: "2025-01-02T00:00:00Z",
|
|
153
|
+
description: "Test app",
|
|
154
|
+
timeZone: "(UTC-08:00) Pacific Time (US & Canada)",
|
|
155
|
+
dateFormat: "MM-DD-YYYY",
|
|
156
|
+
hasEveryoneOnTheInternet: false,
|
|
157
|
+
memoryInfo: { estMemory: 0, estMemoryInclDependentApps: 0 },
|
|
158
|
+
securityProperties: {
|
|
159
|
+
allowClone: false,
|
|
160
|
+
allowExport: true,
|
|
161
|
+
enableAppTokens: true,
|
|
162
|
+
hideFromPublic: false,
|
|
163
|
+
mustBeRealmApproved: false,
|
|
164
|
+
useIPFilter: false,
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
mockFetch.mockResolvedValue({
|
|
169
|
+
ok: true,
|
|
170
|
+
status: 200,
|
|
171
|
+
json: () => Promise.resolve(mockResponse),
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
const startTime = Date.now();
|
|
175
|
+
await Promise.all([
|
|
176
|
+
client.getApp({ appId }),
|
|
177
|
+
client.getApp({ appId }),
|
|
178
|
+
client.getApp({ appId }),
|
|
179
|
+
client.getApp({ appId }),
|
|
180
|
+
client.getApp({ appId }),
|
|
181
|
+
client.getApp({ appId }), // 6th call should be throttled
|
|
182
|
+
]);
|
|
183
|
+
const endTime = Date.now();
|
|
184
|
+
const durationMs = endTime - startTime;
|
|
185
|
+
|
|
186
|
+
console.log(`[Test Debug] mockFetch calls: ${mockFetch.mock.calls.length}`);
|
|
187
|
+
expect(mockFetch).toHaveBeenCalledTimes(6);
|
|
188
|
+
expect(durationMs).toBeGreaterThanOrEqual(200); // 1/5 sec for 6th call
|
|
189
|
+
expect(durationMs).toBeLessThan(1000);
|
|
190
|
+
});
|
|
191
|
+
});
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
createClient,
|
|
4
|
+
mockFetch,
|
|
5
|
+
QB_TABLE_ID_1,
|
|
6
|
+
QB_TABLE_ID_2,
|
|
7
|
+
} from "@tests/setup.ts";
|
|
8
|
+
import {
|
|
9
|
+
CreateRelationship200Response,
|
|
10
|
+
CreateRelationshipRequest,
|
|
11
|
+
} from "/home/drew/Projects/quickbase-js/src/generated/models";
|
|
12
|
+
|
|
13
|
+
describe("QuickbaseClient Unit - createRelationship", () => {
|
|
14
|
+
let client: ReturnType<typeof createClient>;
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
mockFetch.mockClear();
|
|
18
|
+
client = createClient(mockFetch, { debug: true });
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("initializes without errors", () => {
|
|
22
|
+
expect(client).toBeDefined();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("has createRelationship method", () => {
|
|
26
|
+
expect(typeof client.createRelationship).toBe("function");
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("sends correct request and handles successful response", async () => {
|
|
30
|
+
const mockResponse: CreateRelationship200Response = {
|
|
31
|
+
id: 6,
|
|
32
|
+
foreignKeyField: {
|
|
33
|
+
id: 6,
|
|
34
|
+
label: "my relationship field",
|
|
35
|
+
type: "numeric",
|
|
36
|
+
},
|
|
37
|
+
lookupFields: [
|
|
38
|
+
{ id: 7, label: "Field 1", type: "text" },
|
|
39
|
+
{ id: 8, label: "Field 2", type: "text" },
|
|
40
|
+
{ id: 9, label: "Field 3", type: "text" },
|
|
41
|
+
],
|
|
42
|
+
isCrossApp: false,
|
|
43
|
+
parentTableId: QB_TABLE_ID_1,
|
|
44
|
+
childTableId: QB_TABLE_ID_2,
|
|
45
|
+
summaryFields: [
|
|
46
|
+
{ id: 10, label: "my first summary field", type: "numeric" },
|
|
47
|
+
{ id: 11, label: "my second summary field", type: "numeric" },
|
|
48
|
+
],
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
mockFetch.mockResolvedValueOnce({
|
|
52
|
+
ok: true,
|
|
53
|
+
status: 200,
|
|
54
|
+
json: () => Promise.resolve(mockResponse),
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const requestBody: CreateRelationshipRequest = {
|
|
58
|
+
parentTableId: QB_TABLE_ID_1,
|
|
59
|
+
foreignKeyField: { label: "my relationship field" },
|
|
60
|
+
lookupFieldIds: [1, 2, 3],
|
|
61
|
+
summaryFields: [
|
|
62
|
+
{
|
|
63
|
+
summaryFid: 3,
|
|
64
|
+
label: "my first summary field",
|
|
65
|
+
accumulationType: "AVG",
|
|
66
|
+
where: "{'3'.EX.1}",
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
summaryFid: 4,
|
|
70
|
+
label: "my second summary field",
|
|
71
|
+
accumulationType: "SUM",
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const tableId = QB_TABLE_ID_2;
|
|
77
|
+
|
|
78
|
+
const result = await client.createRelationship({
|
|
79
|
+
tableId,
|
|
80
|
+
body: requestBody,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
expect(mockFetch).toHaveBeenCalledTimes(1);
|
|
84
|
+
expect(mockFetch).toHaveBeenCalledWith(
|
|
85
|
+
`https://api.quickbase.com/v1/tables/${QB_TABLE_ID_2}/relationship`,
|
|
86
|
+
expect.objectContaining({
|
|
87
|
+
method: "POST",
|
|
88
|
+
headers: expect.objectContaining({
|
|
89
|
+
"Content-Type": "application/json",
|
|
90
|
+
"QB-Realm-Hostname": "builderprogram-dbradford6815.quickbase.com",
|
|
91
|
+
Authorization:
|
|
92
|
+
"QB-USER-TOKEN b9f3pk_q4jd_0_b4qu5eebyvuix3xs57ysd7zn3",
|
|
93
|
+
}),
|
|
94
|
+
body: JSON.stringify(requestBody),
|
|
95
|
+
})
|
|
96
|
+
);
|
|
97
|
+
expect(result).toEqual(mockResponse);
|
|
98
|
+
expect(result.id).toBe(6);
|
|
99
|
+
expect(result.parentTableId).toBe(QB_TABLE_ID_1);
|
|
100
|
+
expect(result.childTableId).toBe(QB_TABLE_ID_2);
|
|
101
|
+
expect(result.foreignKeyField).toBeDefined();
|
|
102
|
+
if (result.foreignKeyField) {
|
|
103
|
+
expect(result.foreignKeyField.label).toBe("my relationship field");
|
|
104
|
+
}
|
|
105
|
+
expect(result.lookupFields).toHaveLength(3);
|
|
106
|
+
expect(result.summaryFields).toBeDefined();
|
|
107
|
+
if (result.summaryFields) {
|
|
108
|
+
expect(result.summaryFields).toHaveLength(2);
|
|
109
|
+
expect(result.summaryFields[0].label).toBe("my first summary field");
|
|
110
|
+
expect(result.summaryFields[1].label).toBe("my second summary field");
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("handles error from TablesApi", async () => {
|
|
115
|
+
mockFetch.mockResolvedValueOnce({
|
|
116
|
+
ok: false,
|
|
117
|
+
status: 400,
|
|
118
|
+
json: () => Promise.resolve({ message: "Invalid request payload" }),
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
const requestBody: CreateRelationshipRequest = {
|
|
122
|
+
parentTableId: QB_TABLE_ID_1,
|
|
123
|
+
foreignKeyField: { label: "my relationship field" },
|
|
124
|
+
lookupFieldIds: [1, 2, 3],
|
|
125
|
+
summaryFields: [
|
|
126
|
+
{ summaryFid: 3, label: "invalid", accumulationType: "INVALID" },
|
|
127
|
+
],
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
const tableId = QB_TABLE_ID_2;
|
|
131
|
+
|
|
132
|
+
await expect(
|
|
133
|
+
client.createRelationship({
|
|
134
|
+
tableId,
|
|
135
|
+
body: requestBody,
|
|
136
|
+
})
|
|
137
|
+
).rejects.toThrow("API Error: Invalid request payload (Status: 400)");
|
|
138
|
+
|
|
139
|
+
expect(mockFetch).toHaveBeenCalledTimes(1);
|
|
140
|
+
expect(mockFetch).toHaveBeenCalledWith(
|
|
141
|
+
`https://api.quickbase.com/v1/tables/${QB_TABLE_ID_2}/relationship`,
|
|
142
|
+
expect.objectContaining({
|
|
143
|
+
method: "POST",
|
|
144
|
+
headers: expect.objectContaining({
|
|
145
|
+
"Content-Type": "application/json",
|
|
146
|
+
"QB-Realm-Hostname": "builderprogram-dbradford6815.quickbase.com",
|
|
147
|
+
Authorization:
|
|
148
|
+
"QB-USER-TOKEN b9f3pk_q4jd_0_b4qu5eebyvuix3xs57ysd7zn3",
|
|
149
|
+
}),
|
|
150
|
+
body: JSON.stringify(requestBody),
|
|
151
|
+
})
|
|
152
|
+
);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("handles response with undefined foreignKeyField and summaryFields", async () => {
|
|
156
|
+
const mockResponse: CreateRelationship200Response = {
|
|
157
|
+
id: 6,
|
|
158
|
+
foreignKeyField: undefined,
|
|
159
|
+
lookupFields: [
|
|
160
|
+
{ id: 7, label: "Field 1", type: "text" },
|
|
161
|
+
{ id: 8, label: "Field 2", type: "text" },
|
|
162
|
+
{ id: 9, label: "Field 3", type: "text" },
|
|
163
|
+
],
|
|
164
|
+
isCrossApp: false,
|
|
165
|
+
parentTableId: QB_TABLE_ID_1,
|
|
166
|
+
childTableId: QB_TABLE_ID_2,
|
|
167
|
+
summaryFields: undefined,
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
mockFetch.mockResolvedValueOnce({
|
|
171
|
+
ok: true,
|
|
172
|
+
status: 200,
|
|
173
|
+
json: () => Promise.resolve(mockResponse),
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
const requestBody: CreateRelationshipRequest = {
|
|
177
|
+
parentTableId: QB_TABLE_ID_1,
|
|
178
|
+
foreignKeyField: undefined,
|
|
179
|
+
lookupFieldIds: [1, 2, 3],
|
|
180
|
+
summaryFields: undefined,
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const tableId = QB_TABLE_ID_2;
|
|
184
|
+
|
|
185
|
+
const result = await client.createRelationship({
|
|
186
|
+
tableId,
|
|
187
|
+
body: requestBody,
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
expect(mockFetch).toHaveBeenCalledTimes(1);
|
|
191
|
+
expect(mockFetch).toHaveBeenCalledWith(
|
|
192
|
+
`https://api.quickbase.com/v1/tables/${QB_TABLE_ID_2}/relationship`,
|
|
193
|
+
expect.objectContaining({
|
|
194
|
+
method: "POST",
|
|
195
|
+
headers: expect.objectContaining({
|
|
196
|
+
"Content-Type": "application/json",
|
|
197
|
+
"QB-Realm-Hostname": "builderprogram-dbradford6815.quickbase.com",
|
|
198
|
+
Authorization:
|
|
199
|
+
"QB-USER-TOKEN b9f3pk_q4jd_0_b4qu5eebyvuix3xs57ysd7zn3",
|
|
200
|
+
}),
|
|
201
|
+
body: JSON.stringify(requestBody),
|
|
202
|
+
})
|
|
203
|
+
);
|
|
204
|
+
expect(result).toEqual(mockResponse);
|
|
205
|
+
expect(result.id).toBe(6);
|
|
206
|
+
expect(result.parentTableId).toBe(QB_TABLE_ID_1);
|
|
207
|
+
expect(result.childTableId).toBe(QB_TABLE_ID_2);
|
|
208
|
+
expect(result.foreignKeyField).toBeUndefined();
|
|
209
|
+
expect(result.lookupFields).toHaveLength(3);
|
|
210
|
+
expect(result.summaryFields).toBeUndefined();
|
|
211
|
+
});
|
|
212
|
+
});
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// tests/vitest/unit/tables/deleteRelationship.test.ts
|
|
2
|
+
|
|
3
|
+
import { describe, it, expect, beforeEach } from "vitest";
|
|
4
|
+
import {
|
|
5
|
+
createClient,
|
|
6
|
+
mockFetch,
|
|
7
|
+
QB_TABLE_ID_2, // Child table
|
|
8
|
+
} from "@tests/setup.ts";
|
|
9
|
+
|
|
10
|
+
describe("QuickbaseClient Unit - deleteRelationship", () => {
|
|
11
|
+
let client: ReturnType<typeof createClient>;
|
|
12
|
+
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
mockFetch.mockClear();
|
|
15
|
+
client = createClient(mockFetch, { debug: true });
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("initializes without errors", () => {
|
|
19
|
+
expect(client).toBeDefined();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("has deleteRelationship method", () => {
|
|
23
|
+
expect(typeof client.deleteRelationship).toBe("function");
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("sends correct DELETE request and handles successful response", async () => {
|
|
27
|
+
// Mock response for a successful deletion
|
|
28
|
+
const mockResponse = {
|
|
29
|
+
deletedRelationshipId: 6, // Matches the relationshipId
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
mockFetch.mockResolvedValueOnce({
|
|
33
|
+
ok: true,
|
|
34
|
+
status: 200,
|
|
35
|
+
json: () => Promise.resolve(mockResponse),
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const tableId = QB_TABLE_ID_2; // Child table
|
|
39
|
+
const relationshipId = 6; // Example relationship ID (foreign key field ID)
|
|
40
|
+
|
|
41
|
+
const result = await client.deleteRelationship({
|
|
42
|
+
tableId,
|
|
43
|
+
relationshipId,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Verify the fetch call
|
|
47
|
+
expect(mockFetch).toHaveBeenCalledTimes(1);
|
|
48
|
+
expect(mockFetch).toHaveBeenCalledWith(
|
|
49
|
+
`https://api.quickbase.com/v1/tables/${QB_TABLE_ID_2}/relationship/6`,
|
|
50
|
+
expect.objectContaining({
|
|
51
|
+
method: "DELETE",
|
|
52
|
+
headers: expect.objectContaining({
|
|
53
|
+
"Content-Type": "application/json",
|
|
54
|
+
"QB-Realm-Hostname": "builderprogram-dbradford6815.quickbase.com",
|
|
55
|
+
Authorization:
|
|
56
|
+
"QB-USER-TOKEN b9f3pk_q4jd_0_b4qu5eebyvuix3xs57ysd7zn3",
|
|
57
|
+
}),
|
|
58
|
+
})
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
// Verify the response
|
|
62
|
+
expect(result).toEqual(mockResponse);
|
|
63
|
+
expect(result.deletedRelationshipId).toBe(6);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("handles error when relationship is not found", async () => {
|
|
67
|
+
mockFetch.mockResolvedValueOnce({
|
|
68
|
+
ok: false,
|
|
69
|
+
status: 404,
|
|
70
|
+
json: () => Promise.resolve({ message: "Relationship not found" }),
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const tableId = QB_TABLE_ID_2;
|
|
74
|
+
const relationshipId = 999; // Non-existent relationship ID
|
|
75
|
+
|
|
76
|
+
await expect(
|
|
77
|
+
client.deleteRelationship({
|
|
78
|
+
tableId,
|
|
79
|
+
relationshipId,
|
|
80
|
+
})
|
|
81
|
+
).rejects.toThrow("API Error: Relationship not found (Status: 404)");
|
|
82
|
+
|
|
83
|
+
expect(mockFetch).toHaveBeenCalledTimes(1);
|
|
84
|
+
expect(mockFetch).toHaveBeenCalledWith(
|
|
85
|
+
`https://api.quickbase.com/v1/tables/${QB_TABLE_ID_2}/relationship/999`,
|
|
86
|
+
expect.objectContaining({
|
|
87
|
+
method: "DELETE",
|
|
88
|
+
headers: expect.objectContaining({
|
|
89
|
+
"Content-Type": "application/json",
|
|
90
|
+
"QB-Realm-Hostname": "builderprogram-dbradford6815.quickbase.com",
|
|
91
|
+
Authorization:
|
|
92
|
+
"QB-USER-TOKEN b9f3pk_q4jd_0_b4qu5eebyvuix3xs57ysd7zn3",
|
|
93
|
+
}),
|
|
94
|
+
})
|
|
95
|
+
);
|
|
96
|
+
});
|
|
97
|
+
});
|