Migrate to a monorepo structure (#2909)
This commit is contained in:
@ -0,0 +1,23 @@
|
||||
import { computeNewExpirationDate } from '@/settings/developers/utils/compute-new-expiration-date';
|
||||
|
||||
jest.useFakeTimers().setSystemTime(new Date('2024-01-01T00:00:00.000Z'));
|
||||
|
||||
describe('computeNewExpirationDate', () => {
|
||||
it('should compute properly', () => {
|
||||
const expiresAt = '2023-01-10T00:00:00.000Z';
|
||||
const createdAt = '2023-01-01T00:00:00.000Z';
|
||||
const result = computeNewExpirationDate(expiresAt, createdAt);
|
||||
expect(result).toEqual('2024-01-10T00:00:00.000Z');
|
||||
});
|
||||
it('should compute properly with same values', () => {
|
||||
const expiresAt = '2023-01-01T10:00:00.000Z';
|
||||
const createdAt = '2023-01-01T10:00:00.000Z';
|
||||
const result = computeNewExpirationDate(expiresAt, createdAt);
|
||||
expect(result).toEqual('2024-01-01T00:00:00.000Z');
|
||||
});
|
||||
it('should compute properly with no expiration', () => {
|
||||
const createdAt = '2023-01-01T10:00:00.000Z';
|
||||
const result = computeNewExpirationDate(undefined, createdAt);
|
||||
expect(result).toEqual(null);
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,34 @@
|
||||
import { formatExpiration } from '@/settings/developers/utils/format-expiration';
|
||||
|
||||
jest.useFakeTimers().setSystemTime(new Date('2024-01-01T00:00:00.000Z'));
|
||||
|
||||
describe('formatExpiration', () => {
|
||||
it('should format properly when expiresAt is null', () => {
|
||||
const expiresAt = null;
|
||||
const result = formatExpiration(expiresAt);
|
||||
expect(result).toEqual('Never');
|
||||
const resultWithExpiresMention = formatExpiration(expiresAt, true);
|
||||
expect(resultWithExpiresMention).toEqual('Never expires');
|
||||
});
|
||||
it('should format properly when expiresAt is not null', () => {
|
||||
const expiresAt = '2024-01-10T00:00:00.000Z';
|
||||
const result = formatExpiration(expiresAt);
|
||||
expect(result).toEqual('In 9 days');
|
||||
const resultWithExpiresMention = formatExpiration(expiresAt, true);
|
||||
expect(resultWithExpiresMention).toEqual('Expires in 9 days');
|
||||
});
|
||||
it('should format properly when expiresAt is large', () => {
|
||||
const expiresAt = '2034-01-10T00:00:00.000Z';
|
||||
const result = formatExpiration(expiresAt);
|
||||
expect(result).toEqual('In 10 years');
|
||||
const resultWithExpiresMention = formatExpiration(expiresAt, true);
|
||||
expect(resultWithExpiresMention).toEqual('Expires in 10 years');
|
||||
});
|
||||
it('should format properly when expiresAt is large and long version', () => {
|
||||
const expiresAt = '2034-01-10T00:00:00.000Z';
|
||||
const result = formatExpiration(expiresAt, undefined, false);
|
||||
expect(result).toEqual('In 10 years and 9 days');
|
||||
const resultWithExpiresMention = formatExpiration(expiresAt, true, false);
|
||||
expect(resultWithExpiresMention).toEqual('Expires in 10 years and 9 days');
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,14 @@
|
||||
import { DateTime } from 'luxon';
|
||||
|
||||
export const computeNewExpirationDate = (
|
||||
expiresAt: string | null | undefined,
|
||||
createdAt: string,
|
||||
): string | null => {
|
||||
if (!expiresAt) {
|
||||
return null;
|
||||
}
|
||||
const days = DateTime.fromISO(expiresAt).diff(DateTime.fromISO(createdAt), [
|
||||
'days',
|
||||
]).days;
|
||||
return DateTime.utc().plus({ days }).toISO();
|
||||
};
|
||||
@ -0,0 +1,31 @@
|
||||
import { ApiFieldItem } from '@/settings/developers/types/ApiFieldItem';
|
||||
import { ApiKey } from '@/settings/developers/types/ApiKey';
|
||||
import { beautifyDateDiff } from '~/utils/date-utils';
|
||||
|
||||
export const formatExpiration = (
|
||||
expiresAt: string | null,
|
||||
withExpiresMention: boolean = false,
|
||||
short: boolean = true,
|
||||
) => {
|
||||
if (expiresAt) {
|
||||
const dateDiff = beautifyDateDiff(expiresAt, undefined, short);
|
||||
if (dateDiff.includes('-')) {
|
||||
return 'Expired';
|
||||
}
|
||||
return withExpiresMention ? `Expires in ${dateDiff}` : `In ${dateDiff}`;
|
||||
}
|
||||
return withExpiresMention ? 'Never expires' : 'Never';
|
||||
};
|
||||
|
||||
export const formatExpirations = (
|
||||
apiKeys: Array<Pick<ApiKey, 'id' | 'name' | 'expiresAt'>>,
|
||||
): ApiFieldItem[] => {
|
||||
return apiKeys.map(({ id, name, expiresAt }) => {
|
||||
return {
|
||||
id,
|
||||
name,
|
||||
expiration: formatExpiration(expiresAt || null),
|
||||
type: 'internal',
|
||||
};
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user