-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathcommon.ts
More file actions
204 lines (167 loc) · 4.84 KB
/
common.ts
File metadata and controls
204 lines (167 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { GraphQLClient } from 'graphql-request';
import type { QueryKey, UseInfiniteQueryOptions } from '@tanstack/react-query';
import type { GraphQLError } from 'graphql-request/dist/types';
import type { ZodError } from 'zod';
import type { PublicProfile, UserShortProfile } from '../lib/user';
import { graphqlUrl } from '../lib/config';
import type { UserTransactionStatus } from './njord';
// GraphQL Relay pagination types
export type ConnectionCursor = string;
export interface PageInfo {
__typename?: string;
startCursor?: ConnectionCursor | null;
endCursor?: ConnectionCursor | null;
hasPreviousPage?: boolean | null;
hasNextPage?: boolean | null;
totalCount?: number | null;
}
export interface Connection<T> {
__typename?: string;
edges: Array<Edge<T>>;
pageInfo: PageInfo;
}
export interface Edge<T> {
__typename?: string;
node: T;
cursor?: ConnectionCursor;
}
export const DEFAULT_UPVOTES_PER_PAGE = 50;
export interface Upvote {
user: UserShortProfile;
}
export interface UpvotesData {
upvotes: Connection<Upvote>;
}
export interface RequestQueryParams {
[key: string]: unknown;
first: number;
}
export interface RequestQuery<T> {
queryKey: QueryKey;
query: string;
params?: RequestQueryParams;
options?: Omit<Partial<UseInfiniteQueryOptions<T>>, 'select'>;
}
export type RequestDataConnection<TEntity, TKey extends string> = Record<
TKey,
Connection<TEntity>
>;
export const REQUEST_PROTOCOL_KEY = ['request-protocol'];
export interface RequestProtocol {
requestMethod?: typeof gqlClient.request;
fetchMethod?: typeof fetch;
isCompanion?: boolean;
}
const isDeepEqual = (a: unknown, b: unknown): boolean => {
if (a === b) {
return true;
}
if (typeof a !== typeof b) {
return false;
}
if (a === null || b === null) {
return a === b;
}
if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length) {
return false;
}
return a.every((item, i) => isDeepEqual(item, b[i]));
}
if (typeof a === 'object' && typeof b === 'object') {
const keysA = Object.keys(a);
const keysB = Object.keys(b);
if (keysA.length !== keysB.length) {
return false;
}
return keysA.every((key) =>
isDeepEqual(
(a as Record<string, unknown>)[key],
(b as Record<string, unknown>)[key],
),
);
}
return false;
};
export const isQueryKeySame = (left: QueryKey, right: QueryKey): boolean => {
return isDeepEqual(left, right);
};
export enum ApiError {
Forbidden = 'FORBIDDEN',
NotFound = 'NOT_FOUND',
RateLimited = 'RATE_LIMITED',
BalanceTransactionError = 'BALANCE_TRANSACTION_ERROR',
ZodValidationError = 'ZOD_VALIDATION_ERROR',
Conflict = 'CONFLICT',
PaymentRequired = 'PAYMENT_REQUIRED',
Unexpected = 'UNEXPECTED',
}
export enum ApiErrorMessage {
SourcePermissionInviteInvalid = 'SOURCE_PERMISSION_INVITE_INVALID',
}
export const getApiError = (
error: ApiErrorResult,
code: ApiError,
): ApiResponseError =>
error?.response?.errors?.find(({ extensions }) => extensions?.code === code);
interface ApiResponseErrorExtension {
code: ApiError;
}
export interface ApiUserTransactionErrorExtension
extends ApiResponseErrorExtension {
status: UserTransactionStatus;
balance?: {
amount: number;
};
transactionId: string;
}
export interface ApiZodErrorExtension extends ApiResponseErrorExtension {
issues: ZodError['issues'];
}
export interface ApiResponseError<
TExtension extends ApiResponseErrorExtension = ApiResponseErrorExtension,
> {
message: ApiErrorMessage | string;
extensions: TExtension;
}
interface ApiResponse<
TExtension extends ApiResponseErrorExtension = ApiResponseErrorExtension,
> {
errors: ApiResponseError<TExtension>[];
}
export interface ApiErrorResult<
TExtension extends ApiResponseErrorExtension = ApiResponseErrorExtension,
> {
response: ApiResponse<TExtension>;
}
export const GARMR_ERROR = 'GARMR_BROKEN_ERROR';
export const DEFAULT_ERROR = 'An error occurred, please try again';
export const errorMessage = {
profile: {
invalidUsername: 'Invalid character detected! Only underscores (_) allowed',
usernameLength: 'Username must be between 3-38 characters',
invalidHandle: 'Invalid character(s) found in social handle',
invalidSocialLinks:
'Please follow the appropriate format to add your links',
},
};
export interface ReferredUsersData {
referredUsers: Connection<PublicProfile>;
}
export interface MutationError {
data: unknown;
errors: GraphQLError[];
}
export interface ResponseError {
response: MutationError;
}
GraphQLClient.prototype.unsetHeader = function unsetHeader(name: string) {
delete this.options.headers?.[name];
return this;
};
export const gqlClient = new GraphQLClient(graphqlUrl, {
credentials: 'include',
fetch: globalThis.fetch,
});
export const gqlRequest: typeof gqlClient.request = (...args) =>
gqlClient.request(...args);