Files
WT-Offline/apps/server/src/store/store-service.ts
T

51 lines
2.4 KiB
TypeScript

/**
* Store service: routes the game's
* yuplay2 store requests (yuplay-store-api.ts) and the local store page (/store: its files
* from web/, its JSON requests in store-web-api.ts).
*/
import type { AccountStore } from '../accounts/account-store.ts';
import { redactLoginForm } from '../login/form-redaction.ts';
import type { PlayerProfiles } from '../profile/player-profiles.ts';
import type { Service, ServiceRequest, ServiceResponse } from '../transport/exchange.ts';
import { createStaticWebSite, notFound } from '../web-ui/web-helpers.ts';
import type { RedeemTuning } from './redeem-tuning.ts';
import type { StoreCatalogue } from './store-catalogue.ts';
import { StoreWebApi } from './store-web-api.ts';
import { YUPLAY_STORE_PATHS, YuplayStoreApi } from './yuplay-store-api.ts';
const PAGE_PREFIX = '/store';
const STORE_PAGE = createStaticWebSite(new URL('./web/', import.meta.url), [
'index.html', 'styles.css', 'product.css', 'checkout.css', 'app.js', 'products.js', 'gallery.js', 'checkout.js',
]);
const STORE_API_HOSTS = new Set(['api.gaijinent.com', 'store.gaijin.net']);
export class StoreService implements Service {
readonly name = 'store';
readonly #yuplay: YuplayStoreApi;
readonly #web: StoreWebApi;
constructor(accounts: AccountStore, profiles: PlayerProfiles, catalogue: StoreCatalogue, redeemCodes: RedeemTuning, port: number) {
this.#yuplay = new YuplayStoreApi(accounts, profiles, catalogue, redeemCodes, `http://127.0.0.1:${port}${PAGE_PREFIX}`);
this.#web = new StoreWebApi(accounts, profiles, catalogue, redeemCodes);
}
accepts(request: ServiceRequest): boolean {
if (isPagePath(request.path)) return true;
return YUPLAY_STORE_PATHS.has(request.path) && (request.originalHost === undefined || STORE_API_HOSTS.has(request.originalHost));
}
handle(request: ServiceRequest): ServiceResponse {
if (isPagePath(request.path)) {
const subPath = request.path.slice(PAGE_PREFIX.length) || '/';
return this.#web.handle(subPath, request) ?? STORE_PAGE.serve(subPath, 'store page') ?? notFound(`store page 404: ${subPath}`);
}
const redacted = redactLoginForm(request.body);
const response = this.#yuplay.handle(request);
return { ...response, logNotes: { ...response.logNotes, redactedRequestBody: redacted, requestText: redacted } };
}
}
function isPagePath(path: string): boolean {
return path === PAGE_PREFIX || path.startsWith(`${PAGE_PREFIX}/`);
}