fix stuff
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
import * as FileSystem from 'expo-file-system';
|
||||
import type { User, RelationshipData } from '@/constants/Types';
|
||||
import type { User, RelationshipData, Message } from '@/constants/Types';
|
||||
|
||||
export const getInitialDataByAppleId = async (appleId: string) => {
|
||||
try {
|
||||
@ -194,3 +194,52 @@ export const sendRelationshipRequest = async (userId: number, targetUserId: numb
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export const getInitialMessages = async (userId: number, limit: number = 20) => {
|
||||
if (!userId || isNaN(userId)) return;
|
||||
try {
|
||||
const apiUrl = `${process.env.EXPO_PUBLIC_API_URL}/api/messages/getInitialMessages`;
|
||||
const response = await fetch((apiUrl + `?userId=${userId}&limit=${limit}`), {
|
||||
headers: {
|
||||
'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '',
|
||||
},
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(
|
||||
`Error getting initial messages: ${response.status} ${response.statusText}`
|
||||
);
|
||||
}
|
||||
const messages = await response.json() as Message[];
|
||||
return messages;
|
||||
} catch (error: unknown) {
|
||||
console.error('Error getting initial messages:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export const sendMessage = async (message: Message) => {
|
||||
if (!message) return;
|
||||
try {
|
||||
const apiUrl = `${process.env.EXPO_PUBLIC_API_URL}/api/messages/send`;
|
||||
const response = await fetch(apiUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
message: message,
|
||||
}),
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(
|
||||
`Error sending message: ${response.status} ${response.statusText}`
|
||||
);
|
||||
}
|
||||
const messageData = await response.json() as Message;
|
||||
return messageData;
|
||||
} catch (error: unknown) {
|
||||
console.error('Error sending message:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user