slopus--happy
98e40dac97
CLI Smoke Test / smoke-test-linux (20) (push) Has been cancelled
CLI Smoke Test / smoke-test-linux (24) (push) Has been cancelled
CLI Smoke Test / smoke-test-windows (20) (push) Has been cancelled
CLI Smoke Test / smoke-test-windows (24) (push) Has been cancelled
Expo App TypeScript typecheck / typecheck (push) Has been cancelled
87 行
2.8 KiB
TypeScript
87 行
2.8 KiB
TypeScript
import { AuthCredentials } from '@/auth/tokenStorage';
|
|
import { backoff } from '@/utils/time';
|
|
import { z } from 'zod';
|
|
import { getServerUrl } from './serverConfig';
|
|
import { getHappyClientId } from './apiSocket';
|
|
|
|
const PushTokenSchema = z.object({
|
|
id: z.string(),
|
|
token: z.string(),
|
|
createdAt: z.number(),
|
|
updatedAt: z.number(),
|
|
});
|
|
|
|
const PushTokenListResponseSchema = z.object({
|
|
tokens: z.array(PushTokenSchema),
|
|
});
|
|
|
|
export type PushToken = z.infer<typeof PushTokenSchema>;
|
|
|
|
export async function registerPushToken(credentials: AuthCredentials, token: string): Promise<void> {
|
|
const API_ENDPOINT = getServerUrl();
|
|
await backoff(async () => {
|
|
const response = await fetch(`${API_ENDPOINT}/v1/push-tokens`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Authorization': `Bearer ${credentials.token}`,
|
|
'Content-Type': 'application/json',
|
|
'X-Happy-Client': getHappyClientId(),
|
|
},
|
|
body: JSON.stringify({ token })
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`Failed to register push token: ${response.status}`);
|
|
}
|
|
|
|
const data = await response.json();
|
|
if (!data.success) {
|
|
throw new Error('Failed to register push token');
|
|
}
|
|
});
|
|
}
|
|
|
|
export async function fetchPushTokens(credentials: AuthCredentials): Promise<PushToken[]> {
|
|
const API_ENDPOINT = getServerUrl();
|
|
return backoff(async () => {
|
|
const response = await fetch(`${API_ENDPOINT}/v1/push-tokens`, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Authorization': `Bearer ${credentials.token}`,
|
|
'Content-Type': 'application/json',
|
|
'X-Happy-Client': getHappyClientId(),
|
|
}
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`Failed to fetch push tokens: ${response.status}`);
|
|
}
|
|
|
|
const data = await response.json();
|
|
return PushTokenListResponseSchema.parse(data).tokens;
|
|
});
|
|
}
|
|
|
|
export async function unregisterPushToken(credentials: AuthCredentials, token: string): Promise<void> {
|
|
const API_ENDPOINT = getServerUrl();
|
|
await backoff(async () => {
|
|
const response = await fetch(`${API_ENDPOINT}/v1/push-tokens/${encodeURIComponent(token)}`, {
|
|
method: 'DELETE',
|
|
headers: {
|
|
'Authorization': `Bearer ${credentials.token}`,
|
|
'Content-Type': 'application/json',
|
|
'X-Happy-Client': getHappyClientId(),
|
|
}
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`Failed to unregister push token: ${response.status}`);
|
|
}
|
|
|
|
const data = await response.json();
|
|
if (!data.success) {
|
|
throw new Error('Failed to unregister push token');
|
|
}
|
|
});
|
|
}
|