Skip to content

Commit

Permalink
fix: remove env from paths
Browse files Browse the repository at this point in the history
  • Loading branch information
maxakuru committed Oct 23, 2024
1 parent 1b6598f commit eae3ec6
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 29 deletions.
2 changes: 0 additions & 2 deletions src/catalog/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
* governing permissions and limitations under the License.
*/

/* eslint-disable no-await-in-loop */

import { errorResponse } from '../utils/http.js';
import { fetchProduct } from '../utils/r2.js';

Expand Down
2 changes: 1 addition & 1 deletion src/catalog/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default async function catalogHandler(ctx, config, request) {
}

if (pathSegments.length < catalogIndex + 4) {
return errorResponse(400, 'Invalid URL structure: Expected format: /{org}/{site}/{env}/catalog/{store}/{storeView}/product/{sku}');
return errorResponse(400, 'Invalid URL structure: Expected format: /{org}/{site}/catalog/{store}/{storeView}/product/{sku}');
}

const [storeCode, storeViewCode, subRoute, sku] = pathSegments.slice(catalogIndex + 1);
Expand Down
2 changes: 1 addition & 1 deletion src/catalog/lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function handleProductLookupRequest(ctx, config) {
return new Response(undefined, {
status: 301,
headers: {
Location: `${ctx.url.origin}/${config.org}/${config.site}/${config.env}/${config.storeCode}/${config.storeViewCode}/product/${sku}`,
Location: `${ctx.url.origin}/${config.org}/${config.site}/${config.storeCode}/${config.storeViewCode}/product/${sku}`,
},
});
}
Expand Down
23 changes: 5 additions & 18 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,40 +48,28 @@ function extractPathParams(pattern, path) {
* @returns {Promise<Config|null>} - A promise that resolves to the configuration.
*/
export async function resolveConfig(ctx, overrides = {}) {
const [_, org, site, env, route] = ctx.url.pathname.split('/');
const [_, org, site, route] = ctx.url.pathname.split('/');
if (!org) {
throw errorWithResponse(404, 'missing org');
}
if (!site) {
throw errorWithResponse(404, 'missing site');
}
if (!env) {
throw errorWithResponse(404, 'missing env');
}
if (!route) {
throw errorWithResponse(404, 'missing route');
}

const siteKey = `${org}--${site}`;

/**
* @type {ConfigEnvMap}
* @type {ConfigMap}
*/
const confEnvMap = await ctx.env.CONFIGS.get(siteKey, 'json');
if (!confEnvMap) {
return null;
}
if (typeof confEnvMap !== 'object') {
ctx.log.warn('invalid config for ', siteKey);
return null;
}

const confMap = confEnvMap[env];
const confMap = await ctx.env.CONFIGS.get(siteKey, 'json');
if (!confMap) {
return null;
}
if (typeof confMap !== 'object') {
ctx.log.warn('invalid config for ', siteKey, env);
ctx.log.warn('invalid config for ', siteKey);
return null;
}

Expand Down Expand Up @@ -112,10 +100,9 @@ export async function resolveConfig(ctx, overrides = {}) {
headers: confMap.base?.headers ?? {},
params: {},
}),
confEnvMap,
confMap,
org,
site,
env,
route,
...overrides,
};
Expand Down
8 changes: 1 addition & 7 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ declare global {
*/
export type ConfigMap = Record<string, Config>;

/**
* { env => { pathPattern => Config } }
*/
export type ConfigEnvMap = Record<string, ConfigMap>;

/**
* Resolved config object
*/
Expand All @@ -23,14 +18,13 @@ declare global {
apiKey: string;
magentoEnvironmentId: string;
magentoWebsiteCode: string;
env: string;
storeViewCode: string;
storeCode: string;
coreEndpoint: string;
catalogSource: string
catalogEndpoint?: string;
sku?: string;
confEnvMap: ConfigEnvMap;
confMap: ConfigMap;
params: Record<string, string>;
headers: Record<string, string>;
}
Expand Down

0 comments on commit eae3ec6

Please sign in to comment.