Skip to content
Closed
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### Version: 4.10.1
#### Date: Oct-01-2025
Enhancement: Added logHandler interceptors for request and response logging

### Version: 4.10.0
#### Date: Sep-22-2025
Fix: Enhance retry logic to use configured retryDelay
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/delivery-sdk",
"version": "4.10.0",
"version": "4.10.1",
"type": "module",
"license": "MIT",
"main": "./dist/legacy/index.cjs",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export type { ImageTransform } from './lib/image-transform';
export type { AssetQuery } from './lib/asset-query';
export type { TaxonomyQuery } from './lib/taxonomy-query';
export type { ContentTypeQuery } from './lib/contenttype-query';
export type { Taxonomy } from './lib/taxonomy';

export default contentstack;
61 changes: 61 additions & 0 deletions src/lib/contentstack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,67 @@ export function stack(config: StackConfig): StackClass {
});
};
}
// LogHandler interceptors
if (config.debug) {
// Request interceptor for logging
client.interceptors.request.use((requestConfig: any) => {
config.logHandler!('info', {
type: 'request',
method: requestConfig.method?.toUpperCase(),
url: requestConfig.url,
headers: requestConfig.headers,
params: requestConfig.params,
timestamp: new Date().toISOString()
});
return requestConfig;
});

// Response interceptor for logging
client.interceptors.response.use(
(response: any) => {
const level = getLogLevelFromStatus(response.status);
config.logHandler!(level, {
type: 'response',
status: response.status,
statusText: response.statusText,
url: response.config?.url,
method: response.config?.method?.toUpperCase(),
headers: response.headers,
data: response.data,
timestamp: new Date().toISOString()
});
return response;
},
(error: any) => {
const status = error.response?.status || 0;
const level = getLogLevelFromStatus(status);
config.logHandler!(level, {
type: 'response_error',
status: status,
statusText: error.response?.statusText || error.message,
url: error.config?.url,
method: error.config?.method?.toUpperCase(),
error: error.message,
timestamp: new Date().toISOString()
});
throw error;
}
);
}

// Helper function to determine log level based on HTTP status code
function getLogLevelFromStatus(status: number): string {
if (status >= 200 && status < 300) {
return 'info';
} else if (status >= 300 && status < 400) {
return 'warn';
} else if (status >= 400) {
return 'error';
} else {
return 'debug';
}
}

// Retry policy handlers
const errorHandler = (error: any) => {
return retryResponseErrorHandler(error, config, client);
Expand Down
8 changes: 7 additions & 1 deletion src/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { synchronization } from './synchronization';
import {TaxonomyQuery} from './taxonomy-query';
import { GlobalFieldQuery } from './global-field-query';
import { GlobalField } from './global-field';
import { Taxonomy } from './taxonomy';

export class Stack {
readonly config: StackConfig;
Expand All @@ -27,6 +28,7 @@ export class Stack {
* @returns {Asset}
* @example
* import contentstack from '@contentstack/delivery-sdk'
import { Taxonomy } from './taxonomy';
*
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const asset = stack.asset() // For collection of asset
Expand Down Expand Up @@ -77,7 +79,11 @@ export class Stack {

* const taxonomy = stack.taxonomy() // For taxonomy query object
*/
taxonomy(): TaxonomyQuery {
taxonomy(): TaxonomyQuery;
taxonomy(uid: string): Taxonomy;
taxonomy(uid?: string): Taxonomy | TaxonomyQuery {
if (uid) return new Taxonomy(this._client, uid);

return new TaxonomyQuery(this._client);
}

Expand Down
35 changes: 28 additions & 7 deletions src/lib/taxonomy-query.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
import { Query } from "./query";
import { AxiosInstance } from "@contentstack/core";
import { AxiosInstance, getData } from "@contentstack/core";
import { FindResponse } from "./types";

export class TaxonomyQuery extends Query {
constructor(client: AxiosInstance) {
super(client, {}, {}); // will need make changes to Query class so that CT uid is not mandatory
this._client = client;
this._urlPath = `/taxonomies/entries`;
}
};
constructor(client: AxiosInstance) {
super(client, {}, {}); // will need make changes to Query class so that CT uid is not mandatory
this._client = client;
this._urlPath = `/taxonomies/entries`;
}
/**
* @method find
* @memberof TaxonomyQuery
* @description Fetches all taxonomies of the stack using /taxonomy-manager endpoint
* @returns {Promise<FindResponse<T>>}
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const taxonomyQuery = stack.taxonomy();
* const result = await taxonomyQuery.find();
*/
override async find<T>(): Promise<FindResponse<T>> {
this._urlPath = "/taxonomy-manager"; // TODO: change to /taxonomies
const response = await getData(this._client, this._urlPath, {
params: this._queryParams,
});

return response as FindResponse<T>;
}
}
23 changes: 23 additions & 0 deletions src/lib/taxonomy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { AxiosInstance, getData } from '@contentstack/core';

export class Taxonomy {
private _client: AxiosInstance;
private _taxonomyUid: string;
private _urlPath: string;

_queryParams: { [key: string]: string | number } = {};

constructor(client: AxiosInstance, taxonomyUid: string) {
this._client = client;
this._taxonomyUid = taxonomyUid;
this._urlPath = `/taxonomy-manager/${this._taxonomyUid}`; // TODO: change to /taxonomies/${this._taxonomyUid}
}

async fetch<T>(): Promise<T> {
const response = await getData(this._client, this._urlPath);

if (response.taxonomy) return response.taxonomy as T;

return response;
}
}
22 changes: 22 additions & 0 deletions test/api/taxonomy.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-disable no-console */
/* eslint-disable promise/always-return */
import { stackInstance } from '../utils/stack-instance';
import { TTaxonomies } from './types';
import dotenv from 'dotenv';
import { TaxonomyQuery } from '../../src/lib/taxonomy-query';

dotenv.config()

const stack = stackInstance();
describe('ContentType API test cases', () => {
it('should give taxonomies when taxonomies method is called', async () => {
const result = await makeTaxonomy().find<TTaxonomies>();
expect(result).toBeDefined();
});
});

function makeTaxonomy(): TaxonomyQuery {
const taxonomy = stack.taxonomy();

return taxonomy;
}
17 changes: 17 additions & 0 deletions test/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,20 @@ export interface TContentType {
export interface TContentTypes {
content_types: TContentType[];
}

export interface TTaxonomies {
taxonomies: TTaxonomy[];
}

export interface TTaxonomy {
uid: string;
name: string;
description?: string;
terms_count: number;
created_at: string;
updated_at: string;
created_by: string;
updated_by: string;
type: string;
publish_details: PublishDetails;
}
26 changes: 26 additions & 0 deletions test/unit/taxonomy.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { TaxonomyQuery } from '../../src/lib/taxonomy-query';
import { AxiosInstance, httpClient } from '@contentstack/core';
import MockAdapter from 'axios-mock-adapter';
import { taxonomyFindResponseDataMock } from '../utils/mocks';
import { MOCK_CLIENT_OPTIONS } from '../utils/constant';

describe('ta class', () => {
let taxonomy: TaxonomyQuery;
let client: AxiosInstance;
let mockClient: MockAdapter;

beforeAll(() => {
client = httpClient(MOCK_CLIENT_OPTIONS);
mockClient = new MockAdapter(client as any);
});

beforeEach(() => {
taxonomy = new TaxonomyQuery(client);
});

it('should return response data when successful', async () => {
mockClient.onGet('/taxonomy-manager').reply(200, taxonomyFindResponseDataMock); //TODO: change to /taxonomies
const response = await taxonomy.find();
expect(response).toEqual(taxonomyFindResponseDataMock);
});
});
26 changes: 25 additions & 1 deletion test/utils/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1676,6 +1676,29 @@ const gfieldQueryFindResponseDataMock = {
]
}

const taxonomyFindResponseDataMock = {
"taxonomies": [
{
"uid": "taxonomy_testing",
"name": "taxonomy testing",
"description": "",
"terms_count": 1,
"created_at": "2025-10-10T06:42:48.644Z",
"updated_at": "2025-10-10T06:42:48.644Z",
"created_by": "created_by",
"updated_by": "updated_by",
"type": "TAXONOMY",
"ACL": {},
"publish_details": {
"time": "2025-10-10T08:01:48.174Z",
"user": "user",
"environment": "env",
"locale": "en-us"
}
}
]
}

const syncResult: any = { ...axiosGetMock.data };

export {
Expand All @@ -1688,5 +1711,6 @@ export {
entryFindMock,
entryFetchMock,
gfieldFetchDataMock,
gfieldQueryFindResponseDataMock
gfieldQueryFindResponseDataMock,
taxonomyFindResponseDataMock
};