Update examples & uppercase endpoints & fix response (#8391)
Co-authored-by: liruifeng <liruifeng@troila.com>
This commit is contained in:
parent
77922ae5db
commit
d92ab06544
5 changed files with 16 additions and 26 deletions
|
@ -2,7 +2,7 @@ import rss from '@astrojs/rss';
|
||||||
import { getCollection } from 'astro:content';
|
import { getCollection } from 'astro:content';
|
||||||
import { SITE_TITLE, SITE_DESCRIPTION } from '../consts';
|
import { SITE_TITLE, SITE_DESCRIPTION } from '../consts';
|
||||||
|
|
||||||
export async function get(context) {
|
export async function GET(context) {
|
||||||
const posts = await getCollection('blog');
|
const posts = await getCollection('blog');
|
||||||
return rss({
|
return rss({
|
||||||
title: SITE_TITLE,
|
title: SITE_TITLE,
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
// Returns the file body for this non-HTML file.
|
// Returns the file body for this non-HTML file.
|
||||||
// The content type is based off of the extension in the filename,
|
// The content type is based off of the extension in the filename,
|
||||||
// in this case: about.json.
|
// in this case: about.json.
|
||||||
export async function get() {
|
export async function GET() {
|
||||||
return {
|
return new Response(JSON.stringify({
|
||||||
body: JSON.stringify({
|
name: 'Astro',
|
||||||
name: 'Astro',
|
url: 'https://astro.build/',
|
||||||
url: 'https://astro.build/',
|
}));
|
||||||
}),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { APIContext } from 'astro';
|
import { APIContext } from 'astro';
|
||||||
import { userCartItems } from '../../models/session';
|
import { userCartItems } from '../../models/session';
|
||||||
|
|
||||||
export function get({ cookies }: APIContext) {
|
export function GET({ cookies }: APIContext) {
|
||||||
let userId = cookies.get('user-id').value;
|
let userId = cookies.get('user-id').value;
|
||||||
|
|
||||||
if (!userId || !userCartItems.has(userId)) {
|
if (!userId || !userCartItems.has(userId)) {
|
||||||
|
@ -12,9 +12,7 @@ export function get({ cookies }: APIContext) {
|
||||||
let items = userCartItems.get(userId);
|
let items = userCartItems.get(userId);
|
||||||
let array = Array.from(items.values());
|
let array = Array.from(items.values());
|
||||||
|
|
||||||
return {
|
return new Response(JSON.stringify({ items: array }));
|
||||||
body: JSON.stringify({ items: array }),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface AddToCartItem {
|
interface AddToCartItem {
|
||||||
|
@ -22,7 +20,7 @@ interface AddToCartItem {
|
||||||
name: string;
|
name: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function post({ cookies, request }: APIContext) {
|
export async function POST({ cookies, request }: APIContext) {
|
||||||
const item: AddToCartItem = await request.json();
|
const item: AddToCartItem = await request.json();
|
||||||
|
|
||||||
let userId = cookies.get('user-id').value;
|
let userId = cookies.get('user-id').value;
|
||||||
|
@ -38,9 +36,7 @@ export async function post({ cookies, request }: APIContext) {
|
||||||
cart.set(item.id, { id: item.id, name: item.name, count: 1 });
|
cart.set(item.id, { id: item.id, name: item.name, count: 1 });
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return new Response(JSON.stringify({
|
||||||
body: JSON.stringify({
|
ok: true,
|
||||||
ok: true,
|
}));
|
||||||
}),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
import { products } from '../../models/db';
|
import { products } from '../../models/db';
|
||||||
|
|
||||||
export function get() {
|
export function GET() {
|
||||||
return {
|
return new Response(JSON.stringify(products));
|
||||||
body: JSON.stringify(products),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,12 @@
|
||||||
import { productMap } from '../../../models/db';
|
import { productMap } from '../../../models/db';
|
||||||
import type { APIContext } from 'astro';
|
import type { APIContext } from 'astro';
|
||||||
|
|
||||||
export function get({ params }: APIContext) {
|
export function GET({ params }: APIContext) {
|
||||||
const id = Number(params.id);
|
const id = Number(params.id);
|
||||||
if (productMap.has(id)) {
|
if (productMap.has(id)) {
|
||||||
const product = productMap.get(id);
|
const product = productMap.get(id);
|
||||||
|
|
||||||
return {
|
return new Response(JSON.stringify(products));
|
||||||
body: JSON.stringify(product),
|
|
||||||
};
|
|
||||||
} else {
|
} else {
|
||||||
return new Response(null, {
|
return new Response(null, {
|
||||||
status: 400,
|
status: 400,
|
||||||
|
|
Loading…
Add table
Reference in a new issue