Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create utils package #905

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ module.exports = {
'./packages/ui/tsconfig.json',
'./packages/ui/tsconfig.test.json',
'./packages/upl/tsconfig.json',
'./packages/utils/tsconfig.json',
'./packages/utils/tsconfig.test.json',
],
tsconfigRootDir: __dirname,
},
Expand Down
1 change: 1 addition & 0 deletions packages/utils/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'rollup-plugin-peer-deps-external';
19 changes: 19 additions & 0 deletions packages/utils/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { Config } from 'jest';

const config: Config = {
preset: 'ts-jest',
// to obtain access to the matchers.
moduleFileExtensions: ['ts', 'js', 'json', 'node'],
modulePaths: ['<rootDir>'],
testEnvironment: 'node',
transform: {
'^.+\\.(ts)$': [
'ts-jest',
{
tsconfig: 'tsconfig.test.json',
},
],
},
};

export default config;
56 changes: 56 additions & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"name": "@frameless/utils",
"version": "0.0.0",
"description": "A shared utils library",
"main": "./dist/index.cjs.js",
"module": "./dist/index.esm.js",
"types": "./dist/src/index.d.ts",
"private": true,
"files": [
"dist/"
],
"keywords": [],
"repository": {
"type": "git+ssh",
"url": "[email protected]:frameless/strapi.git",
"directory": "packages/utils"
},
"publishConfig": {
"registry": "https://npm.pkg.github.com/"
},
"author": {
"name": "@frameless"
},
"engines": {
"node": "20.x.x"
},
"license": "EUPL-1.2",
"peerDependencies": {},
"scripts": {
"prebuild": "yarn clean",
"build": "rollup --config rollup.config.ts --configPlugin typescript --bundleConfigAsCjs",
"watch": "rollup --config rollup.config.ts --configPlugin typescript -w --bundleConfigAsCjs",
"clean": "rimraf dist .rollup.cach dist",
"test": "jest --coverage",
"test:watch": "jest --watch",
"lint-build": "tsc --noEmit --project tsconfig.json"
},
"devDependencies": {
"@rollup/plugin-commonjs": "25.0.8",
"@rollup/plugin-json": "6.1.0",
"@rollup/plugin-node-resolve": "15.3.0",
"@rollup/plugin-typescript": "11.1.6",
"@types/jest": "29.5.12",
"jest": "29.7.0",
"rimraf": "6.0.1",
"rollup": "3.29.5",
"rollup-plugin-copy": "3.5.0",
"rollup-plugin-peer-deps-external": "2.2.4",
"rollup-plugin-terser": "7.0.2",
"rollup-plugin-typescript2": "0.36.0",
"ts-jest": "29.2.3",
"ts-node": "10.9.2",
"typescript": "5.0.4"
},
"dependencies": {}
}
44 changes: 44 additions & 0 deletions packages/utils/rollup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import { readFileSync } from 'node:fs';
import { RollupOptions } from 'rollup';
import copy from 'rollup-plugin-copy';
import peerDepsExternal from 'rollup-plugin-peer-deps-external';
import { terser } from 'rollup-plugin-terser';
import typescript from 'rollup-plugin-typescript2';

const packageJson = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8'));
export const outputGlobals = {};

const config: RollupOptions = {
input: 'src/index.ts', // Entry point to your library
output: [
{
file: packageJson.main,
format: 'cjs', // CommonJS format
exports: 'auto', // Automatic exports for CommonJS
globals: outputGlobals, // Global variables exposed to the browser
},
{
file: packageJson.module,
format: 'esm', // ES Module format
globals: outputGlobals,
},
],
plugins: [
typescript({
tsconfig: 'tsconfig.json',
}),
nodeResolve(),
commonjs(),
terser(), // Minify the output
peerDepsExternal(), // Treat peer dependencies as externals
copy({
targets: [{ src: 'src/types/*.d.ts', dest: 'dist/types' }], // Copy type declarations to the dist folder
}),
json(),
],
};

export default config;
47 changes: 47 additions & 0 deletions packages/utils/src/errorHandler/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { ErrorHandler } from './index';

describe('ErrorHandler', () => {
it('should create an instance of ErrorHandler', () => {
const error = new ErrorHandler('Test error', { statusCode: 500 });
expect(error).toBeInstanceOf(ErrorHandler);
expect(error.message).toBe('Test error');
expect(error.options?.statusCode).toBe(500);
});
it('should create an instance of ErrorHandler with isOperational', () => {
const error = new ErrorHandler('Test error', { statusCode: 500 });
expect(error).toBeInstanceOf(ErrorHandler);
expect(error.message).toBe('Test error');
expect(error.options?.statusCode).toBe(500);
expect(error.isOperational).toBe(true);
});
it('should log the error message and status code', () => {
const error = new ErrorHandler('Test error', { statusCode: 500 });
expect(error).toBeInstanceOf(ErrorHandler);
expect(error.message).toBe('Test error');
expect(error.options?.statusCode).toBe(500);
});
it('should create an instance of ErrorHandler without options', () => {
const error = new ErrorHandler('Test error');
expect(error).toBeInstanceOf(ErrorHandler);
expect(error.message).toBe('Test error');
expect(error.options).toBeUndefined();
});
it('should create an instance of ErrorHandler without message and options', () => {
const error = new ErrorHandler();
expect(error).toBeInstanceOf(ErrorHandler);
expect(error.message).toBe('');
expect(error.options).toBeUndefined();
});
it('should create an instance of ErrorHandler with only message', () => {
const error = new ErrorHandler('Test error');
expect(error).toBeInstanceOf(ErrorHandler);
expect(error.message).toBe('Test error');
expect(error.options).toBeUndefined();
});
it('should create an instance of ErrorHandler with only options', () => {
const error = new ErrorHandler(undefined, { statusCode: 500 });
expect(error).toBeInstanceOf(ErrorHandler);
expect(error.message).toBe('');
expect(error.options?.statusCode).toBe(500);
});
});
16 changes: 16 additions & 0 deletions packages/utils/src/errorHandler/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export type Options = {
statusCode: number;
};
export class ErrorHandler extends Error {
isOperational: boolean; // this flag for custom error identification

constructor(
message?: string,
public options?: Options,
) {
super(message);
this.name = 'ErrorHandler';
this.options = options;
this.isOperational = true; // Operational errors should be marked
}
}
Loading
Loading