Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
61 changes: 60 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import { TimeoutError } from "./src/errors/TimeoutError.js";


// Load configuration from environment variables
const { baseURL, authType, credentials, oath2Config, redisConfig, accessTokenURL } = getConfig();
const { baseURL, authType, credentials, oath2Config, redisConfig, accessTokenURL, companyName, companyId } = getConfig();

// Create an authentication handler
const authHandler = CreateAuthHandler(authType, credentials, oath2Config, accessTokenURL, redisConfig);

// Initialize the transport utility
const transport = new Transport({ baseURL, cacheTTL: 600 }, authHandler);

transport.addMiddleware(async (config) => {
const token = await authHandler.getAccessToken();
if (!token) return config;
Expand All @@ -26,6 +27,64 @@ transport.addMiddleware(async (config) => {
config.headers['Authorization'] = `Bearer ${token}`;
return config;
});

transport.addMiddleware(async (config) => {
const company = config?.headers?.['X-Custom-Request-Company'];
const whichCompanyIdentifier = config?.headers?.['X-Custom-Request-Company-Identifier'];
config.headers = config.headers ?? {}
if (company) {
if (whichCompanyIdentifier) {
switch (whichCompanyIdentifier) {
case 'Company-Name': {
config.params = config.params ?? {};
config.params.company = company;
config.headers['X-Custom-Params-Company-Command'] = 'Skip'
break;
};
case 'Company-Id': {
const splittedUrl = config?.url?.split("/");
if (splittedUrl && Array.isArray(splittedUrl)) {
const last = splittedUrl[splittedUrl.length - 1];
const url = `${splittedUrl.slice(0, -1).join("/")}/companies(${company})/${last}`;
config.url = url;
config.headers = config.headers ?? {}
config.headers['X-Custom-Params-Company-Command'] = 'Remove'
}
break;
};
default: {
config.headers['X-Custom-Params-Company-Command'] = 'Set'
}
}
}
} else {
if (whichCompanyIdentifier) {
switch (whichCompanyIdentifier) {
case 'Company-Name': {
config.params = config.params ?? {};
config.params.company = companyName;
config.headers['X-Custom-Params-Company-Command'] = 'Skip'
break;
};
case 'Company-Id': {
const splittedUrl = config?.url?.split("/")
if (splittedUrl && Array.isArray(splittedUrl)) {
const last = splittedUrl[splittedUrl.length - 1];
const url = `${splittedUrl.slice(0, -1).join("/")}/companies(${companyId})/${last}`;
config.url = url;
config.headers = config.headers ?? {}
config.headers['X-Custom-Params-Company-Command'] = 'Remove'
}
break;
};
default: {
config.headers['X-Custom-Params-Company-Command'] = 'Set'
}
}
}
}
return config;
});
export {
Transport,
CreateAuthHandler,
Expand Down
40 changes: 31 additions & 9 deletions src/transport/Transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,30 @@ export class Transport {
if (config.params) {
config.params = { ...config.params };
}
if (!config.params.company) {
config.params.company = this.defaultCompany
config.headers = config.headers ?? {};
const companyParamCommand = config.headers['X-Custom-Params-Company-Command'];

switch (companyParamCommand) {
case 'Set': {
if (!config.params.company) {
config.params.company = this.defaultCompany;
}
break;
};
case 'Skip': {
break
}
case 'Remove': {
if (config.params.company) {
delete config.params.company;
}
break;
}
default: {
if (!config.params.company) {
config.params.company = this.defaultCompany;
}
}
}
return config;
})
Expand Down Expand Up @@ -183,15 +205,15 @@ export class Transport {
}
async patch<T>(endpoint: string, payload?: any, options?: RequestOptions): Promise<T> {
try {
const primaryKeys = this.extractPrimaryKeys(payload, options?.primaryKey);
const primaryKeys = this.extractPrimaryKeys(payload, options?.primaryKeys);
const url = `${endpoint}(${primaryKeys.join(',')})`;
if (options?.headers) {
options.headers['If-Match'] = "*";
} else {
options = { ...options, headers: { 'If-Match': "*" } };
}
if (options.primaryKey) {
for (const key of options.primaryKey) {
if (options.primaryKeys) {
for (const key of options.primaryKeys) {
delete payload[key];
}
}
Expand All @@ -202,15 +224,15 @@ export class Transport {
}
async put<T>(endpoint: string, payload?: any, options?: RequestOptions): Promise<T> {
try {
const primaryKeys = this.extractPrimaryKeys(payload, options?.primaryKey);
const primaryKeys = this.extractPrimaryKeys(payload, options?.primaryKeys);
const url = `${endpoint}(${primaryKeys.join(',')})`;
if (options?.headers) {
options.headers['If-Match'] = "*";
} else {
options = { ...options, headers: { 'If-Match': "*" } };
}
if (options.primaryKey) {
for (const key of options.primaryKey) {
if (options.primaryKeys) {
for (const key of options.primaryKeys) {
delete payload[key];
}
}
Expand All @@ -222,7 +244,7 @@ export class Transport {
async delete<T>(endpoint: string, payload?: any, options?: RequestOptions): Promise<T> {
try {
this.invalidateCache(endpoint, options);
const primaryKeys = this.extractPrimaryKeys(payload, options?.primaryKey);
const primaryKeys = this.extractPrimaryKeys(payload, options?.primaryKeys);
const url = `${endpoint}(${primaryKeys.join(',')})`;
if (options?.headers) {
options.headers['If-Match'] = "*";
Expand Down
8 changes: 7 additions & 1 deletion tests/unit/oauth2.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import { transport } from "../../index.ts";

async function runTest() {
try {
const response = await transport.get("/api/KineticTechnology/CashMgt/v2.0/imprestAPI", {}, { headers: { 'Prefer': "maxpagesize=2" } }) as Response;
const response = await transport.get("/api/nurture/ESS/v1.0/leavemployees", {}, {
headers: {
'Prefer': "maxpagesize=2",
'X-Custom-Request-Company-Identifier': 'Company-Id',
'X-Custom-Request-Company': "083db09a-ff98-f011-a7b2-6045bdacc0b6"
}
}) as Response;
// const filter = await transport.filter({
// date_from: "2022-01-01",
// date_to: '2022-01-01',
Expand Down