Skip to content

Commit 81e7a08

Browse files
committed
update a bunchof things
1 parent cdced81 commit 81e7a08

File tree

31 files changed

+834
-85
lines changed

31 files changed

+834
-85
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { type Entry, type NewEntry, type Tag, type NewTag, type EntryTag, type NewEntryTag, type EntryWithTags, type User } from './schema.js';
2+
export declare class DBClient {
3+
private baseUrl;
4+
private oauthToken?;
5+
constructor(baseUrl: string, oauthToken?: string);
6+
private makeRequest;
7+
createEntry(entry: NewEntry): Promise<EntryWithTags>;
8+
getEntry(id: number): Promise<EntryWithTags | null>;
9+
getEntries(options?: {
10+
tagIds?: number[];
11+
from?: string;
12+
to?: string;
13+
}): Promise<Array<{
14+
id: number;
15+
title: string;
16+
tagCount: number;
17+
}>>;
18+
updateEntry(id: number, entry: Partial<NewEntry>): Promise<EntryWithTags>;
19+
deleteEntry(id: number): Promise<boolean>;
20+
createTag(tag: NewTag): Promise<Tag>;
21+
getTag(id: number): Promise<Tag | null>;
22+
getTags(): Promise<Array<{
23+
id: number;
24+
name: string;
25+
}>>;
26+
updateTag(id: number, tag: Partial<NewTag>): Promise<Tag>;
27+
deleteTag(id: number): Promise<boolean>;
28+
addTagToEntry(entryTag: NewEntryTag): Promise<EntryTag>;
29+
getEntryTags(entryId: number): Promise<Tag[]>;
30+
getUserById(id: number): Promise<User | null>;
31+
}
32+
export type { Entry, NewEntry, Tag, NewTag, EntryTag, NewEntryTag, EntryWithTags, User, };
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
var __assign = (this && this.__assign) || function () {
2+
__assign = Object.assign || function(t) {
3+
for (var s, i = 1, n = arguments.length; i < n; i++) {
4+
s = arguments[i];
5+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6+
t[p] = s[p];
7+
}
8+
return t;
9+
};
10+
return __assign.apply(this, arguments);
11+
};
12+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
14+
return new (P || (P = Promise))(function (resolve, reject) {
15+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
16+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
17+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
18+
step((generator = generator.apply(thisArg, _arguments || [])).next());
19+
});
20+
};
21+
var __generator = (this && this.__generator) || function (thisArg, body) {
22+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
23+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
24+
function verb(n) { return function (v) { return step([n, v]); }; }
25+
function step(op) {
26+
if (f) throw new TypeError("Generator is already executing.");
27+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
28+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
29+
if (y = 0, t) op = [op[0] & 2, t.value];
30+
switch (op[0]) {
31+
case 0: case 1: t = op; break;
32+
case 4: _.label++; return { value: op[1], done: false };
33+
case 5: _.label++; y = op[1]; op = [0]; continue;
34+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
35+
default:
36+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
37+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
38+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
39+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
40+
if (t[2]) _.ops.pop();
41+
_.trys.pop(); continue;
42+
}
43+
op = body.call(thisArg, _);
44+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
45+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46+
}
47+
};
48+
import { z } from 'zod';
49+
// Response schemas
50+
var apiResponseSchema = z.object({
51+
result: z.any(),
52+
});
53+
var errorResponseSchema = z.object({
54+
error: z.string(),
55+
details: z.array(z.any()).optional(),
56+
});
57+
var DBClient = /** @class */ (function () {
58+
function DBClient(baseUrl, oauthToken) {
59+
this.baseUrl = baseUrl.replace(/\/$/, ''); // Remove trailing slash
60+
this.oauthToken = oauthToken;
61+
}
62+
DBClient.prototype.makeRequest = function (method_1) {
63+
return __awaiter(this, arguments, void 0, function (method, params) {
64+
var response, errorData, error, data, parsed;
65+
if (params === void 0) { params = {}; }
66+
return __generator(this, function (_a) {
67+
switch (_a.label) {
68+
case 0: return [4 /*yield*/, fetch("".concat(this.baseUrl, "/db-api"), {
69+
method: 'POST',
70+
headers: {
71+
'Content-Type': 'application/json',
72+
Authorization: this.oauthToken ? "Bearer ".concat(this.oauthToken) : '',
73+
},
74+
body: JSON.stringify({
75+
method: method,
76+
params: params,
77+
}),
78+
})];
79+
case 1:
80+
response = _a.sent();
81+
if (!!response.ok) return [3 /*break*/, 3];
82+
return [4 /*yield*/, response.json().catch(function () { return ({}); })];
83+
case 2:
84+
errorData = _a.sent();
85+
error = errorResponseSchema.safeParse(errorData);
86+
if (error.success) {
87+
throw new Error(error.data.error);
88+
}
89+
throw new Error("HTTP ".concat(response.status, ": ").concat(response.statusText));
90+
case 3: return [4 /*yield*/, response.json()];
91+
case 4:
92+
data = _a.sent();
93+
parsed = apiResponseSchema.parse(data);
94+
return [2 /*return*/, parsed.result];
95+
}
96+
});
97+
});
98+
};
99+
// Entry Methods
100+
DBClient.prototype.createEntry = function (entry) {
101+
return __awaiter(this, void 0, void 0, function () {
102+
return __generator(this, function (_a) {
103+
return [2 /*return*/, this.makeRequest('createEntry', entry)];
104+
});
105+
});
106+
};
107+
DBClient.prototype.getEntry = function (id) {
108+
return __awaiter(this, void 0, void 0, function () {
109+
return __generator(this, function (_a) {
110+
return [2 /*return*/, this.makeRequest('getEntry', { id: id })];
111+
});
112+
});
113+
};
114+
DBClient.prototype.getEntries = function (options) {
115+
return __awaiter(this, void 0, void 0, function () {
116+
return __generator(this, function (_a) {
117+
return [2 /*return*/, this.makeRequest('getEntries', options || {})];
118+
});
119+
});
120+
};
121+
DBClient.prototype.updateEntry = function (id, entry) {
122+
return __awaiter(this, void 0, void 0, function () {
123+
return __generator(this, function (_a) {
124+
return [2 /*return*/, this.makeRequest('updateEntry', __assign({ id: id }, entry))];
125+
});
126+
});
127+
};
128+
DBClient.prototype.deleteEntry = function (id) {
129+
return __awaiter(this, void 0, void 0, function () {
130+
return __generator(this, function (_a) {
131+
return [2 /*return*/, this.makeRequest('deleteEntry', { id: id })];
132+
});
133+
});
134+
};
135+
// Tag Methods
136+
DBClient.prototype.createTag = function (tag) {
137+
return __awaiter(this, void 0, void 0, function () {
138+
return __generator(this, function (_a) {
139+
return [2 /*return*/, this.makeRequest('createTag', tag)];
140+
});
141+
});
142+
};
143+
DBClient.prototype.getTag = function (id) {
144+
return __awaiter(this, void 0, void 0, function () {
145+
return __generator(this, function (_a) {
146+
return [2 /*return*/, this.makeRequest('getTag', { id: id })];
147+
});
148+
});
149+
};
150+
DBClient.prototype.getTags = function () {
151+
return __awaiter(this, void 0, void 0, function () {
152+
return __generator(this, function (_a) {
153+
return [2 /*return*/, this.makeRequest('getTags', {})];
154+
});
155+
});
156+
};
157+
DBClient.prototype.updateTag = function (id, tag) {
158+
return __awaiter(this, void 0, void 0, function () {
159+
return __generator(this, function (_a) {
160+
return [2 /*return*/, this.makeRequest('updateTag', __assign({ id: id }, tag))];
161+
});
162+
});
163+
};
164+
DBClient.prototype.deleteTag = function (id) {
165+
return __awaiter(this, void 0, void 0, function () {
166+
return __generator(this, function (_a) {
167+
return [2 /*return*/, this.makeRequest('deleteTag', { id: id })];
168+
});
169+
});
170+
};
171+
// Entry Tag Methods
172+
DBClient.prototype.addTagToEntry = function (entryTag) {
173+
return __awaiter(this, void 0, void 0, function () {
174+
return __generator(this, function (_a) {
175+
return [2 /*return*/, this.makeRequest('addTagToEntry', entryTag)];
176+
});
177+
});
178+
};
179+
DBClient.prototype.getEntryTags = function (entryId) {
180+
return __awaiter(this, void 0, void 0, function () {
181+
return __generator(this, function (_a) {
182+
return [2 /*return*/, this.makeRequest('getEntryTags', { entryId: entryId })];
183+
});
184+
});
185+
};
186+
// User Methods
187+
DBClient.prototype.getUserById = function (id) {
188+
return __awaiter(this, void 0, void 0, function () {
189+
return __generator(this, function (_a) {
190+
return [2 /*return*/, this.makeRequest('getUserById', { id: id })];
191+
});
192+
});
193+
};
194+
return DBClient;
195+
}());
196+
export { DBClient };

0 commit comments

Comments
 (0)