Skip to content

Commit

Permalink
Use process.env in a more performant way
Browse files Browse the repository at this point in the history
  • Loading branch information
pozylon committed Jan 9, 2025
1 parent 775b762 commit f599aad
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
4 changes: 3 additions & 1 deletion packages/api/src/express/createERCMetadataMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { Request, RequestHandler } from 'express';

const logger = createLogger('unchained:erc-metadata');

const { ROOT_URL = 'http://localhost:4010' } = process.env;

const errorHandler = (res, e) => {
logger.error(e.message);
res.writeHead(503);
Expand All @@ -29,7 +31,7 @@ const ercMetadataMiddleware: RequestHandler = async (
}

const { services, localeContext } = req.unchainedContext;
const url = new URL(req.url, process.env.ROOT_URL);
const url = new URL(req.url, ROOT_URL);
const parsedPath = path.parse(url.pathname);

if (parsedPath.ext !== '.json') throw new Error('Invalid ERC Metadata URI');
Expand Down
4 changes: 3 additions & 1 deletion packages/api/src/fastify/ercMetadataHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { FastifyRequest, RouteHandlerMethod } from 'fastify';

const logger = createLogger('unchained:erc-metadata');

const { ROOT_URL = 'http://localhost:4010' } = process.env;

const errorHandler = (res) => (e) => {
logger.error(e.message);
res.status(503);
Expand All @@ -22,7 +24,7 @@ const ercMetadataHandler: RouteHandlerMethod = async (
) => {
try {
const { services, localeContext } = req.unchainedContext;
const url = new URL(req.url, process.env.ROOT_URL);
const url = new URL(req.url, ROOT_URL);

if (!url.pathname.toLowerCase().endsWith('.json')) throw new Error('Invalid ERC Metadata URI');

Expand Down
6 changes: 4 additions & 2 deletions packages/core-files/src/module/configureFilesModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { filesSettings, FilesSettingsOptions } from '../files-settings.js';

const FILE_EVENTS: string[] = ['FILE_CREATE', 'FILE_UPDATE', 'FILE_REMOVE'];

const { ROOT_URL = 'http://localhost:4010' } = process.env;

export const configureFilesModule = async ({
db,
options: filesOptions = {},
Expand All @@ -23,8 +25,8 @@ export const configureFilesModule = async ({
if (URL.canParse(transformedURLString)) {
const finalURL = new URL(transformedURLString);
return finalURL.href;
} else if (URL.canParse(transformedURLString, process.env.ROOT_URL)) {
const finalURL = new URL(transformedURLString, process.env.ROOT_URL);
} else if (URL.canParse(transformedURLString, ROOT_URL)) {
const finalURL = new URL(transformedURLString, ROOT_URL);
return finalURL.href;
}
return transformedURLString;
Expand Down
4 changes: 2 additions & 2 deletions packages/plugins/src/warehousing/eth-minter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { systemLocale } from '@unchainedshop/utils';
import { generateDbObjectId } from '@unchainedshop/mongodb';
import { getFileAdapter } from '@unchainedshop/core-files';

const { MINTER_TOKEN_OFFSET = '0' } = process.env;
const { MINTER_TOKEN_OFFSET = '0', ROOT_URL = 'http://localhost:4010' } = process.env;

const ETHMinter: IWarehousingAdapter = {
...WarehousingAdapter,
Expand Down Expand Up @@ -127,7 +127,7 @@ const ETHMinter: IWarehousingAdapter = {
const isDefaultLanguageActive = locale ? locale?.language === systemLocale.language : true;
const localization = isDefaultLanguageActive
? {
uri: `${process.env.ROOT_URL}/erc-metadata/${product._id}/{locale}/${tokenId}.json`,
uri: `${ROOT_URL}/erc-metadata/${product._id}/{locale}/${tokenId}.json`,
default: systemLocale.language,
locales: allLanguages.map((lang) => lang.isoCode),
}
Expand Down

0 comments on commit f599aad

Please sign in to comment.