Wave 6, the last substantive piece of the mock-API migration. Wave 1 removed the brand facet and the sales/comments sorts for want of a model; sales turn out to be derivable from order_items and a brand model is a table plus a column. - a `brands` table with a nullable `products.brand_id` and an ordered admin replace, mirroring categories and storefront content; a public `GET /api/brands` and a `brand_id` filter on the catalog, which the search page's facet uses - `sold_count` per product, computed from `order_items` joined to orders that reached payment, so an abandoned or cancelled checkout cannot count as a sale. It is computed per read rather than stored, so it cannot drift from the orders that produced it - `sort=sales` alongside `sort=price`; anything else is still a 400 - merchants can set a product's brand through the existing product upsert - the review UI is gone: the card's review figure and the product detail page's reviews tab, summary and replies. There is no reviews model, and the mall attributed invented comments to named shoppers and showed a "good rate". The now-unreferenced fabrication helpers went with it (`salesOf`, `commentCountOf`, `commentsFor`, `commentStats`, `salesRankFor`, `productDetail`, `storeDetail`) Two bugs found by checking rather than trusting: the fixed-data `listProducts` had silently ignored `brand_id`, `sort` and `order`, so the restored facet rendered but filtered nothing until the rollback check caught it; and the seed's brand lookup read back through the shared `r` variable the product loop reassigns, working once and then throwing. Verified: 29 backend tests green including a new brand-and-sales case; all three frontends build; searching filters by brand (24 to 6) and sorts by sales with counts matching the API; a product page offers detail and after-sale tabs only, with a real sold count; the fixed-data rollback filters by brand too. OpenSpec change: openspec/changes/replace-mock-api-wave-6
99 lines
3.0 KiB
TypeScript
99 lines
3.0 KiB
TypeScript
import { createApi } from "@vmall/shared";
|
|
import type { ApiClient } from "@vmall/shared";
|
|
import { createMockApi } from "~/mock/api";
|
|
|
|
/**
|
|
* Domains the live backend serves. Every other domain stays on the fixed-data
|
|
* adapter, so a domain can be migrated - or rolled back - by editing this list
|
|
* alone. See openspec/changes/replace-mock-api-wave-1/design.md.
|
|
*/
|
|
type LiveDomain =
|
|
| "auth"
|
|
| "catalog"
|
|
| "currency"
|
|
| "content"
|
|
| "shops"
|
|
| "brands"
|
|
| "cart"
|
|
| "orders"
|
|
| "shipments"
|
|
| "invoices";
|
|
|
|
/**
|
|
* Explicit per-domain method picks rather than a string allowlist: indexing
|
|
* `ApiClient` by a union of method names would need an unsafe cast, and this
|
|
* keeps the compiler checking that every picked key exists.
|
|
*/
|
|
const LIVE_PICKS = {
|
|
auth: (a: ApiClient) => ({ register: a.register, login: a.login, me: a.me }),
|
|
catalog: (a: ApiClient) => ({
|
|
listProducts: a.listProducts,
|
|
getProduct: a.getProduct,
|
|
listCategories: a.listCategories,
|
|
}),
|
|
currency: (a: ApiClient) => ({ listCurrencies: a.listCurrencies, convert: a.convert }),
|
|
content: (a: ApiClient) => ({ getHomeContent: a.getHomeContent }),
|
|
shops: (a: ApiClient) => ({ listShops: a.listShops, getShop: a.getShop }),
|
|
brands: (a: ApiClient) => ({ listBrands: a.listBrands }),
|
|
cart: (a: ApiClient) => ({
|
|
getCart: a.getCart,
|
|
addCartItem: a.addCartItem,
|
|
updateCartItem: a.updateCartItem,
|
|
removeCartItem: a.removeCartItem,
|
|
}),
|
|
orders: (a: ApiClient) => ({
|
|
checkout: a.checkout,
|
|
listMyOrders: a.listMyOrders,
|
|
getOrder: a.getOrder,
|
|
cancelOrder: a.cancelOrder,
|
|
payOrder: a.payOrder,
|
|
}),
|
|
shipments: (a: ApiClient) => ({
|
|
confirmDelivered: a.confirmDelivered,
|
|
listMyShipments: a.listMyShipments,
|
|
}),
|
|
invoices: (a: ApiClient) => ({
|
|
requestInvoice: a.requestInvoice,
|
|
listMyInvoices: a.listMyInvoices,
|
|
}),
|
|
} satisfies Record<LiveDomain, (a: ApiClient) => Partial<ApiClient>>;
|
|
|
|
const KNOWN_DOMAINS = Object.keys(LIVE_PICKS) as LiveDomain[];
|
|
|
|
/** Fallback when runtimeConfig supplies no list; keep in step with nuxt.config.ts. */
|
|
const DEFAULT_LIVE_DOMAINS: LiveDomain[] = [
|
|
"catalog",
|
|
"currency",
|
|
"content",
|
|
"shops",
|
|
"brands",
|
|
"auth",
|
|
"cart",
|
|
"orders",
|
|
"shipments",
|
|
"invoices",
|
|
];
|
|
|
|
export default defineNuxtPlugin(() => {
|
|
const config = useRuntimeConfig();
|
|
const mock = createMockApi();
|
|
const live = createApi({
|
|
baseUrl: config.public.apiBase as string,
|
|
getToken: () => (import.meta.client ? localStorage.getItem("vmall.token") : null),
|
|
});
|
|
|
|
const configured = config.public.liveDomains as string[] | undefined;
|
|
const liveDomains = (configured ?? DEFAULT_LIVE_DOMAINS).filter((domain): domain is LiveDomain =>
|
|
KNOWN_DOMAINS.includes(domain as LiveDomain),
|
|
);
|
|
|
|
// The fixed-data adapter is the base object, so an unmigrated domain cannot
|
|
// regress and a live domain can be rolled back by removing one entry.
|
|
let api: ApiClient = mock;
|
|
for (const domain of liveDomains) {
|
|
api = { ...api, ...LIVE_PICKS[domain](live) };
|
|
}
|
|
|
|
return { provide: { api } };
|
|
});
|