0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-13 22:11:20 -05:00
astro/examples/ssr/src/api.ts
Daniel 5923dd77c1
adding cookies to api route response [simple result] (#5060)
* adding cookies to the an api route response, also when returning a simple result

* in dev server, convert a simple endpoint result into a response object

Co-authored-by: AirBorne04 <unknown>
Co-authored-by: AirBorne04 <>
Co-authored-by: Matthew Phillips <matthew@skypack.dev>
2022-10-18 09:52:49 -04:00

82 lines
1.9 KiB
TypeScript

export interface Product {
id: number;
name: string;
price: number;
image: string;
}
interface User {
id: number;
}
interface Cart {
items: Array<{
id: number;
name: string;
count: number;
}>;
}
function getOrigin(request: Request): string {
return new URL(request.url).origin.replace('localhost', '127.0.0.1');
}
async function get<T>(
incomingReq: Request,
endpoint: string,
cb: (response: Response) => Promise<T>
): Promise<T> {
const response = await fetch(`${getOrigin(incomingReq)}${endpoint}`, {
credentials: 'same-origin',
headers: incomingReq.headers,
});
if (!response.ok) {
// TODO make this better...
return null;
}
return cb(response);
}
export async function getProducts(incomingReq: Request): Promise<Product[]> {
return get<Product[]>(incomingReq, '/api/products', async (response) => {
const products: Product[] = await response.json();
return products;
});
}
export async function getProduct(incomingReq: Request, id: number): Promise<Product> {
return get<Product>(incomingReq, `/api/products/${id}`, async (response) => {
const product: Product = await response.json();
return product;
});
}
export async function getUser(incomingReq: Request): Promise<User> {
return get<User>(incomingReq, `/api/user`, async (response) => {
const user: User = await response.json();
return user;
});
}
export async function getCart(incomingReq: Request): Promise<Cart> {
return get<Cart>(incomingReq, `/api/cart`, async (response) => {
const cart: Cart = await response.json();
return cart;
});
}
export async function addToUserCart(id: number | string, name: string): Promise<void> {
await fetch(`${location.origin}/api/cart`, {
credentials: 'same-origin',
method: 'POST',
mode: 'no-cors',
headers: {
'Content-Type': 'application/json',
Cache: 'no-cache',
},
body: JSON.stringify({
id,
name,
}),
});
}