Skip to content

Commit

Permalink
feat: create utils package
Browse files Browse the repository at this point in the history
  • Loading branch information
AliKdhim87 committed Sep 27, 2024
1 parent 77e15ac commit cfe281a
Show file tree
Hide file tree
Showing 13 changed files with 392 additions and 6 deletions.
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;
41 changes: 41 additions & 0 deletions packages/utils/src/errorHandler/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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 log the error message and status code', () => {
const error = new ErrorHandler('Test error', { statusCode: 500 });
const spy = jest.spyOn(console, 'log');
error.logger();
expect(spy).toHaveBeenCalledWith({ message: 'Test error', statusCode: 500 });
spy.mockRestore();
});
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);
});
});
22 changes: 22 additions & 0 deletions packages/utils/src/errorHandler/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export type Options = {
statusCode: number;
};

export class ErrorHandler extends Error {
constructor(
message?: string,
public options?: Options,
) {
super(message);
this.name = 'ErrorHandler';
this.options = options;
}
// this logger method should be used only on the server side to give some information when something goes wrong
logger() {
// eslint-disable-next-line no-console
console.log({
message: this?.message,
statusCode: this?.options?.statusCode,
});
}
}
7 changes: 7 additions & 0 deletions packages/utils/src/get-strapi-plugin-name/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { getStrapiPluginName } from './index';

describe('getStrapiPluginName', () => {
it('should return the plugin name without the @frameless/strapi-plugin- prefix', () => {
expect(getStrapiPluginName('@frameless/strapi-plugin-example')).toBe('example');
});
});
11 changes: 11 additions & 0 deletions packages/utils/src/get-strapi-plugin-name/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// describe the function
/**
* @param {string} name - The name of the plugin
* @returns {string} The name of the plugin without the prefix
* @example
* getStrapiPluginName('@frameless/strapi-plugin-preview-button') // 'preview-button'
*
* */

export const getStrapiPluginName = (name: string): string =>
name && name.replace(/^@frameless\/(@[^-,.][\w,-]+\/|strapi-)plugin-/i, '');
3 changes: 3 additions & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { ErrorHandler } from './errorHandler';
export { fetchData } from './fetchData';
export { getStrapiPluginName } from './get-strapi-plugin-name';
27 changes: 27 additions & 0 deletions packages/utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"allowUnreachableCode": false,
"composite": true,
"declaration": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"forceConsistentCasingInFileNames": true,
"incremental": true,
"isolatedModules": true,
"lib": ["dom", "es2020"],
"module": "es2020",
"moduleResolution": "node",
"noUnusedLocals": true,
"noUnusedParameters": true,
"outDir": "dist",
"resolveJsonModule": true,
"rootDir": ".",
"skipLibCheck": true,
"strict": true,
"target": "es2020"
},
"include": ["src/**/*.ts", "./rollup.config.ts", "./global.d.ts"],
"exclude": ["**/node_modules/*", "**/*.test.ts"]
}
12 changes: 12 additions & 0 deletions packages/utils/tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "ES2016",
"module": "ES6",
"moduleResolution": "node",
"esModuleInterop": true,
"skipLibCheck": true,
"strict": true,
"types": ["jest"]
},
"include": ["**/*.test.tsx", "**/*.test.ts", "./jest.config.ts", "tests", "src"]
}
Loading

0 comments on commit cfe281a

Please sign in to comment.