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
1 change: 1 addition & 0 deletions packages/contentstack-utilities/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"traverse": "^0.6.11",
"tty-table": "^4.2.3",
"unique-string": "^2.0.0",
"short-uuid": "^6.0.0",
"uuid": "^14.0.0",
"winston": "^3.19.0",
"xdg-basedir": "^4.0.0"
Expand Down
10 changes: 10 additions & 0 deletions packages/contentstack-utilities/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { checkSync } from 'recheck';
import traverse from 'traverse';
import { v4 as uuidV4 } from 'uuid';
import { generate as shortUuidGenerate } from 'short-uuid';
import authHandler from './auth-handler';
import { ContentstackClient, HttpClient, cliux, configHandler } from '.';
import { hasProxy, getProxyUrl } from './proxy-helper';
Expand Down Expand Up @@ -278,6 +280,14 @@ export function getAuthenticationMethod(): string {
* @param authenticationMethod - Optional authentication method
* @returns Context object with all session-level metadata
*/
export function generateUid(): string {
return uuidV4();
}

export function generateShortUid(): string {
return shortUuidGenerate();
}

export function createLogContext(
commandId: string,
apiKey: string,
Expand Down
31 changes: 30 additions & 1 deletion packages/contentstack-utilities/test/unit/helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cliux, validatePath, getBranchFromAlias } from '../../lib';
import { cliux, validatePath, getBranchFromAlias, generateUid, generateShortUid } from '../../lib';
import { expect } from 'chai';
import { fancy } from 'fancy-test';

Expand Down Expand Up @@ -246,3 +246,32 @@ describe('Testing the getBranchFromAlias function', () => {
});
});
});

describe('generateUid', () => {
it('should return a string', () => {
expect(generateUid()).to.be.a('string');
});

it('should return a valid UUID v4 format', () => {
const uuidV4Regex = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
expect(generateUid()).to.match(uuidV4Regex);
});

it('should return a unique value on each call', () => {
expect(generateUid()).to.not.equal(generateUid());
});
});

describe('generateShortUid', () => {
it('should return a string', () => {
expect(generateShortUid()).to.be.a('string');
});

it('should return a non-empty string', () => {
expect(generateShortUid().length).to.be.greaterThan(0);
});

it('should return a unique value on each call', () => {
expect(generateShortUid()).to.not.equal(generateShortUid());
});
});
3 changes: 1 addition & 2 deletions packages/contentstack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@
"@oclif/plugin-plugins": "^5.4.54",
"figlet": "1.8.1",
"node-machine-id": "^1.1.12",
"semver": "^7.7.2",
"short-uuid": "^6.0.0"
"semver": "^7.7.2"
},
"overrides": {
"@oclif/core": {
Expand Down
5 changes: 2 additions & 3 deletions packages/contentstack/src/utils/context-handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as shortUUID from 'short-uuid';
import * as path from 'path';
import { configHandler, pathValidator, sanitizePath } from '@contentstack/cli-utilities';
import { configHandler, pathValidator, sanitizePath, generateShortUid } from '@contentstack/cli-utilities';
import { machineIdSync } from 'node-machine-id';

export default class CsdxContext {
Expand Down Expand Up @@ -33,7 +32,7 @@ export default class CsdxContext {
configHandler.set('clientId', this.clientId);
}
analyticsInfo.push(this.clientId);
const sessionId = shortUUID.generate();
const sessionId = generateShortUid();
configHandler.set('sessionId', sessionId);
this.sessionId = sessionId;
analyticsInfo.push(this.sessionId);
Expand Down
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

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

Loading