Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
5d7b97f
feat: add environment variable configuration with @fastify/env
JoelFickson Feb 13, 2025
c2c2e9e
feat: add IngridClientOptions interface and configure environment var…
JoelFickson Feb 13, 2025
7d73fd2
feat: introduce IngridClientOptions interface and update client confi…
JoelFickson Feb 13, 2025
a9668c1
feat: implement IngridClientOptions interface for improved client con…
JoelFickson Feb 13, 2025
801c3ef
feat: define IngridClientOptions interface for client configuration
JoelFickson Feb 13, 2025
e40896c
feat: update baseURL in createClient function to use IngridBasePath m…
JoelFickson Feb 13, 2025
be2c793
feat: remove dotenv dependency and update logger to use environment v…
JoelFickson Feb 13, 2025
54784f6
feat: remove dotenv dependency and configure logger to use environmen…
JoelFickson Feb 13, 2025
b35c36f
feat: add typebox schema for environment variables
JoelFickson Feb 13, 2025
ba88550
feat: remove console log from IngridApiClient constructor and add log…
JoelFickson Feb 14, 2025
9c2f3ba
feat: update logger to remove default projectKey fallback and enhance…
JoelFickson Feb 14, 2025
a745721
Merge branch 'refs/heads/main' into chore/refactor-types
JoelFickson Feb 25, 2025
d246b66
feat: add keyOfIngridSessionCustomType to environment variable schema…
JoelFickson Feb 25, 2025
fd45f77
Merge branch 'refs/heads/main' into chore/refactor-types
JoelFickson Feb 26, 2025
148ce39
feat(ingrid): update Ingrid client configuration and enhance error ha…
JoelFickson Feb 26, 2025
ff9f6c8
chore: fomart ingrid shipping service
JoelFickson Feb 26, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 68 additions & 1 deletion processor/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion processor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
"@commercetools/ts-client": "^3.0.4",
"@fastify/autoload": "6.1.0",
"@fastify/cors": "10.0.2",
"@fastify/env": "^5.0.2",
"@fastify/formbody": "8.0.2",
"@fastify/http-proxy": "11.0.1",
"@fastify/request-context": "6.0.2",
"@fastify/static": "8.1.1",
"@fastify/type-provider-typebox": "5.1.0",
"@sinclair/typebox": "0.34.27",
"axios": "^1.7.9",
"dotenv": "16.4.7",
"fastify": "5.2.1",
"fastify-plugin": "5.0.1"
},
Expand Down
7 changes: 4 additions & 3 deletions processor/src/clients/ingrid/ingrid.client.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import axios, { AxiosInstance } from 'axios';
import {
IngridBasePath,
IngridGetSessionResponse,
IngridUrls,
type IngridClientOptions,
type IngridCompleteSessionRequestPayload,
type IngridCompleteSessionResponse,
type IngridCreateSessionRequestPayload,
type IngridCreateSessionResponse,
IngridGetSessionResponse,
type IngridUpdateSessionRequestPayload,
type IngridUpdateSessionResponse,
IngridUrls,
} from './types/ingrid.client.type';
import { AbstractIngridClient } from './abstract-ingrid.client';
import { CustomError } from '../../libs/fastify/errors';
Expand All @@ -34,7 +35,7 @@ import { CustomError } from '../../libs/fastify/errors';
export class IngridApiClient implements AbstractIngridClient {
private client: AxiosInstance;

constructor(opts: { apiSecret: string; environment: keyof typeof IngridBasePath }) {
constructor(opts: IngridClientOptions) {
this.client = createClient(opts);
}

Expand Down
5 changes: 5 additions & 0 deletions processor/src/clients/ingrid/types/ingrid.client.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export enum IngridUrls {
DELIVERY_CHECKOUT = '/v1/delivery_checkout',
}

export interface IngridClientOptions {
apiSecret: string;
environment: IngridEnvironment;
}

/**
* Response type for getting an Ingrid session
* @see {@link https://developer.ingrid.com/delivery_checkout/reference/#GetSessionResponse}
Expand Down
19 changes: 0 additions & 19 deletions processor/src/config/index.ts

This file was deleted.

3 changes: 1 addition & 2 deletions processor/src/libs/logger/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createApplicationLogger } from '@commercetools-backend/loggers';
import { getRequestContext } from '../fastify/context/context';
import { getConfig } from '../../config';
import setIn from 'lodash/set';
import { format } from 'logform';
import cloneDeep from 'lodash/cloneDeep';
Expand Down Expand Up @@ -30,7 +29,7 @@ const defaultFieldsFormatter = (defaults: DefaultFields) => {
export const appLogger = createApplicationLogger({
formatters: [
defaultFieldsFormatter({
projectKey: getConfig().projectKey,
projectKey: process.env.CTP_PROJECT_KEY,
version: process.env.npm_package_version,
name: process.env.npm_package_name,
correlationId: () => getRequestContext().correlationId,
Expand Down
3 changes: 0 additions & 3 deletions processor/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import * as dotenv from 'dotenv';
dotenv.config();

import { setupFastify } from './server/server';

(async () => {
Expand Down
78 changes: 78 additions & 0 deletions processor/src/server/Env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { FastifyInstance } from 'fastify';
import fastifyEnv from '@fastify/env';
import { Static, Type } from '@sinclair/typebox';

const schema = {
type: 'object',
required: [
'CTP_PROJECT_KEY',
'CTP_CLIENT_ID',
'CTP_CLIENT_SECRET',
'CTP_AUTH_URL',
'CTP_API_URL',
'CTP_SESSION_URL',
'LOGGER_LEVEL',
'INGRID_API_KEY',
'INGRID_ENVIRONMENT',
'npm_package_version',
'npm_package_name',
'keyOfIngridSessionCustomType',
],
properties: {
CTP_PROJECT_KEY: { type: 'string' },
CTP_CLIENT_ID: { type: 'string' },
CTP_CLIENT_SECRET: { type: 'string' },
CTP_AUTH_URL: { type: 'string' },
CTP_API_URL: { type: 'string' },
CTP_SESSION_URL: { type: 'string' },
LOGGER_LEVEL: { type: 'string', default: 'info' },
INGRID_API_KEY: { type: 'string' },
INGRID_ENVIRONMENT: { type: 'string', enum: ['STAGING', 'PRODUCTION'] },
npm_package_version: { type: 'string' },
npm_package_name: { type: 'string' },
keyOfIngridSessionCustomType: { type: 'string' },
},
};

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const typeboxSchema = Type.Object({
CTP_PROJECT_KEY: Type.String(),
CTP_CLIENT_ID: Type.String(),
CTP_CLIENT_SECRET: Type.String(),
CTP_AUTH_URL: Type.String(),
CTP_API_URL: Type.String(),
CTP_SESSION_URL: Type.String(),
LOGGER_LEVEL: Type.String(),
INGRID_API_KEY: Type.String(),
INGRID_ENVIRONMENT: Type.Enum({
STAGING: 'STAGING',
PRODUCTION: 'PRODUCTION',
}),
npm_package_version: Type.String(),
npm_package_name: Type.String(),
keyOfIngridSessionCustomType: Type.String(),
});

export type Env = Static<typeof typeboxSchema>;

const options = {
confKey: 'environmentVariables',
schema: schema,
dotenv: true,
};

/**
* Configures environment variables for the Fastify server using @fastify/env plugin.
* This function registers environment variables according to the defined schema and
* makes them available through server.config.
*/
export async function configureEnvironmentVariables(server: FastifyInstance): Promise<Env> {
await server.register(fastifyEnv, options);
return server.environmentVariables as Env;
}

declare module 'fastify' {
interface FastifyInstance {
environmentVariables: Env;
}
}
20 changes: 10 additions & 10 deletions processor/src/server/plugins/ingrid-shipping.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import { FastifyInstance } from 'fastify';
import { shippingRoutes } from '../../routes/ingrid-shipping.route';
import { IngridShippingService } from '../../services/ingrid-shipping.service';
import { IngridApiClient } from '../../clients/ingrid/ingrid.client';
import { getConfig } from '../../config';
import { CommercetoolsApiClient } from '../../clients/commercetools/api.client';
import { appLogger } from '../../libs/logger';
import { RequestContextData, updateRequestContext, getRequestContext } from '../../libs/fastify/context';
import { SessionHeaderAuthInitializer } from '../../libs/auth';
import { IngridClientOptions } from '../../clients/ingrid/types/ingrid.client.type';

export default async function (server: FastifyInstance) {
const opts = {
clientId: getConfig().clientId,
clientSecret: getConfig().clientSecret,
authUrl: getConfig().authUrl,
apiUrl: getConfig().apiUrl,
projectKey: getConfig().projectKey,
sessionUrl: getConfig().sessionUrl,
clientId: server.environmentVariables.CTP_CLIENT_ID,
clientSecret: server.environmentVariables.CTP_CLIENT_SECRET,
authUrl: server.environmentVariables.CTP_AUTH_URL,
apiUrl: server.environmentVariables.CTP_API_URL,
projectKey: server.environmentVariables.CTP_PROJECT_KEY,
sessionUrl: server.environmentVariables.CTP_SESSION_URL,
logger: appLogger,
getContextFn: (): RequestContextData => {
const { correlationId, requestId, authentication } = getRequestContext();
Expand All @@ -36,9 +36,9 @@ export default async function (server: FastifyInstance) {
},
};

const ingridOpts = {
apiSecret: getConfig().ingridApiKey,
environment: getConfig().ingridEnvironment as 'STAGING' | 'PRODUCTION',
const ingridOpts: IngridClientOptions = {
apiSecret: server.environmentVariables.INGRID_API_KEY,
environment: server.environmentVariables.INGRID_ENVIRONMENT,
};

const commercetoolsApiClient: CommercetoolsApiClient = new CommercetoolsApiClient(opts);
Expand Down
6 changes: 4 additions & 2 deletions processor/src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import fastifyFormBody from '@fastify/formbody';
import Fastify from 'fastify';
import { randomUUID } from 'node:crypto';
import { join } from 'path';
import { getConfig } from '../config';
import { requestContextPlugin } from '../libs/fastify/context';
import { errorHandler } from '../libs/fastify/error-handler';
import { configureEnvironmentVariables } from './Env';

/**
* Setup Fastify server instance
Expand All @@ -16,13 +16,15 @@ export const setupFastify = async () => {
// Create fastify server instance
const server = Fastify({
logger: {
level: getConfig().loggerLevel,
level: process.env.LOGGER_LEVEL || 'info',
},
genReqId: () => randomUUID().toString(),
requestIdLogLabel: 'requestId',
requestIdHeader: 'x-request-id',
});

await configureEnvironmentVariables(server);

// Setup error handler
server.setErrorHandler(errorHandler);

Expand Down
3 changes: 1 addition & 2 deletions processor/src/services/ingrid-shipping.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
} from './helpers';
import { Cart } from '@commercetools/platform-sdk';
import { InitSessionResponse, UpdateSessionResponse } from './types/ingrid-shipping.type';
import { getConfig } from '../config';

export class IngridShippingService extends AbstractShippingService {
constructor(commercetoolsClient: CommercetoolsApiClient, ingridClient: IngridApiClient) {
Expand All @@ -26,7 +25,7 @@ export class IngridShippingService extends AbstractShippingService {
* @returns {Promise<InitSessionResponse>} Returns the commercetools cart id, ingrid session id and ingrid checkout session html snippet
*/
public async init(): Promise<InitSessionResponse> {
const ingridSessionCustomTypeKey = getConfig().keyOfIngridSessionCustomType;
const ingridSessionCustomTypeKey = process.env.INGRID_SESSION_CUSTOM_TYPE_KEY || 'ingrid-session';
const customType = await this.commercetoolsClient.getCustomType(ingridSessionCustomTypeKey);

if (!customType) {
Expand Down
Loading