Skip to content

Commit 28e0409

Browse files
committed
another one attempt to dual-type mongoId
1 parent 14a964a commit 28e0409

File tree

1 file changed

+45
-16
lines changed

1 file changed

+45
-16
lines changed

api.ts

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
/* ----- api/v1 ----- */
22

3-
/** test comment */
4-
export type ItemId = string;
3+
export type ObjectId = string &
4+
Partial<{
5+
toHexString(): string;
6+
}>;
57

6-
export interface BaseItem {
7-
_id: ItemId;
8+
export type ItemIdV1 = ObjectId;
9+
10+
export type ItemId = ItemIdV1;
811

12+
export interface Fresh {
913
name: string;
1014
description?: string;
1115

1216
owner: ItemId;
1317
group: ItemId;
1418
}
1519

20+
export type Staged = Fresh & {
21+
_id: ItemId;
22+
};
23+
24+
export type Base = Staged;
25+
1626

1727
/* ----- api/v1/models ----- */
1828

@@ -67,7 +77,7 @@ export type ModelProps = {
6777
idPattern?: string;
6878
};
6979

70-
export interface Model extends BaseItem {
80+
export interface Model extends Base {
7181
base: string;
7282
data: ModelNode;
7383
props?: ModelProps;
@@ -81,7 +91,7 @@ export type ServiceState = {
8191
_gid: ItemId;
8292
time: number;
8393
online: boolean;
84-
}
94+
};
8595

8696
export type BaseState = ServiceState & {
8797
[argumentId: string]: number | boolean | string | BaseState;
@@ -94,7 +104,7 @@ export type BaseConfig = {
94104
};
95105

96106
export interface RicObject<TState = BaseState, TConfig = BaseConfig>
97-
extends BaseItem {
107+
extends Base {
98108
id: string;
99109
model: ItemId;
100110

@@ -127,7 +137,7 @@ export interface Event<T = unknown> {
127137

128138
/* ----- api/v1/index ----- */
129139
export interface WellKnown {
130-
base: BaseItem;
140+
base: Base;
131141

132142
models: Model;
133143
objects: RicObject;
@@ -148,13 +158,16 @@ declare var require: any;
148158
export const VERSION = "v1";
149159
export const DEFAULT_BASE_URL = "https://dev.rightech.io/";
150160

151-
export type DeprecatedResponseFields = {
161+
export type DeprecatedResponseV1 = {
152162
codes: string[];
153163
success: boolean;
154-
}
164+
};
165+
166+
export type DeprecatedResponseV2 = {};
167+
export type DeprecatedResponse = DeprecatedResponseV1 & DeprecatedResponseV2;
155168

156169
export type ApiErrorHelper = {
157-
message?: string;
170+
message: string;
158171
links?: string[];
159172
};
160173

@@ -171,7 +184,14 @@ function unique<T = unknown>(array: T[] = []) {
171184
});
172185
}
173186

174-
export class ApiError extends Error {
187+
export interface ApiError extends Error {
188+
url: string;
189+
code: number;
190+
tags: string[];
191+
helper: ApiErrorHelper;
192+
}
193+
194+
export class ApiError extends Error implements ApiError {
175195
url: string = "";
176196
verb: string = "GET";
177197
tags: string[] = [];
@@ -197,7 +217,7 @@ export class ApiError extends Error {
197217
}
198218

199219
withHelper(helper: ApiErrorHelper) {
200-
this.helper = helper;
220+
this.helper = { ...this.helper, ...(helper || {}) };
201221
if (this.helper && this.helper.message) {
202222
this.message = `${this.message}. ${this.helper.message}`;
203223
}
@@ -224,7 +244,7 @@ export class ApiError extends Error {
224244
static fromJson(opts: RequestOptions, json: ApiError, statusCode = 500) {
225245
return new ApiError(json.message)
226246
.withCode(statusCode)
227-
.withHelper(json.helper || {})
247+
.withHelper(json.helper)
228248
.withVerb(opts.method)
229249
.withUrl(opts.url)
230250
.withTags(json.tags);
@@ -594,7 +614,16 @@ export interface MoreTypedClient {
594614
): Promise<T>;
595615
}
596616

597-
export class Client {
617+
export interface Client {
618+
new (opts?: ClientOpts): Client;
619+
620+
get<T = unknown>(path: string): Promise<T[]>;
621+
post<T = unknown>(path: string, data: Partial<T>): Promise<T>;
622+
patch<T = unknown>(path: string, data: Partial<T>): Promise<T>;
623+
delete<T = unknown>(path: string): Promise<T>;
624+
}
625+
626+
export class Client implements Client {
598627
_opts: ClientOpts;
599628
url: string;
600629
token: string;
@@ -686,7 +715,7 @@ export class Client {
686715
}
687716
}
688717

689-
export function getDefaultClient(opts?: ClientOpts) : MoreTypedClient & Client {
718+
export function getDefaultClient(opts?: ClientOpts): MoreTypedClient & Client {
690719
const maybeNode = (globalThis as any)["process"] as any;
691720
if (maybeNode && maybeNode.env) {
692721
return new Client({

0 commit comments

Comments
 (0)