-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
77e15ac
commit cfe281a
Showing
13 changed files
with
392 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
declare module 'rollup-plugin-peer-deps-external'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ''); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
Oops, something went wrong.