Skip to content

Commit

Permalink
feat(client): add client package (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangsijie authored Jul 12, 2022
1 parent f21dccf commit 04c7b56
Show file tree
Hide file tree
Showing 12 changed files with 1,465 additions and 6 deletions.
10 changes: 10 additions & 0 deletions packages/client/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Config } from '@jest/types';

const config: Config.InitialOptions = {
preset: 'ts-jest',
collectCoverageFrom: ['src/**/*.ts'],
coverageReporters: ['lcov', 'text-summary'],
setupFilesAfterEnv: ['<rootDir>/jest.setup.js', 'jest-matcher-specific-error'],
};

export default config;
15 changes: 15 additions & 0 deletions packages/client/jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Need to disable following rules to mock text-decode/text-encoder and crypto for jsdom
// https://github.com/jsdom/jsdom/issues/1612
/* eslint-disable unicorn/prefer-module */
// TODO: crypto related to linear issue LOG-1517
const { Crypto } = require('@peculiar/webcrypto');
const fetch = require('node-fetch');
const { TextDecoder, TextEncoder } = require('text-encoder');
/* eslint-enable unicorn/prefer-module */

/* eslint-disable @silverhand/fp/no-mutation */
global.crypto = new Crypto();
global.fetch = fetch;
global.TextDecoder = TextDecoder;
global.TextEncoder = TextEncoder;
/* eslint-enable @silverhand/fp/no-mutation */
73 changes: 73 additions & 0 deletions packages/client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"name": "@logto/client",
"version": "1.0.0-alpha.2",
"source": "./src/index.ts",
"main": "./lib/index.js",
"exports": {
"require": "./lib/index.js",
"import": "./lib/module.js"
},
"module": "./lib/module.js",
"types": "./lib/index.d.ts",
"files": [
"lib"
],
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/logto-io/js.git",
"directory": "packages/client"
},
"scripts": {
"dev:tsc": "tsc -p tsconfig.build.json -w --preserveWatchOutput",
"precommit": "lint-staged",
"check": "tsc --noEmit",
"build": "rm -rf lib/ && pnpm check && parcel build",
"lint": "eslint --ext .ts src",
"test": "jest",
"test:coverage": "jest --silent --env=jsdom && jest --silent --coverage",
"prepack": "pnpm test"
},
"dependencies": {
"@logto/js": "^1.0.0-alpha.2",
"@silverhand/essentials": "^1.1.6",
"camelcase-keys": "^7.0.1",
"jose": "^4.3.8",
"js-base64": "^3.7.2",
"lodash.get": "^4.4.2",
"lodash.once": "^4.1.1",
"superstruct": "^0.16.0"
},
"devDependencies": {
"@jest/types": "^27.5.1",
"@parcel/core": "^2.6.2",
"@parcel/packager-ts": "^2.6.2",
"@parcel/transformer-typescript-types": "^2.6.2",
"@peculiar/webcrypto": "^1.1.7",
"@silverhand/eslint-config": "^0.14.0",
"@silverhand/ts-config": "^0.14.0",
"@types/jest": "^27.4.1",
"@types/lodash.get": "^4.4.6",
"@types/lodash.once": "^4.1.7",
"@types/node": "^17.0.19",
"eslint": "^8.9.0",
"jest": "^27.5.1",
"jest-matcher-specific-error": "^1.0.0",
"lint-staged": "^13.0.0",
"nock": "^13.1.3",
"node-fetch": "^2.6.7",
"parcel": "^2.6.2",
"prettier": "^2.3.2",
"text-encoder": "^0.0.4",
"ts-jest": "^27.0.4",
"type-fest": "^2.10.0",
"typescript": "^4.5.5"
},
"eslintConfig": {
"extends": "@silverhand"
},
"prettier": "@silverhand/eslint-config/.prettierrc",
"publishConfig": {
"access": "public"
}
}
36 changes: 36 additions & 0 deletions packages/client/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { NormalizeKeyPaths } from '@silverhand/essentials';
import get from 'lodash.get';

const logtoClientErrorCodes = Object.freeze({
sign_in_session: {
invalid: 'Invalid sign-in session.',
not_found: 'Sign-in session not found.',
},
not_authenticated: 'Not authenticated.',
get_access_token_by_refresh_token_failed: 'Failed to get access token by refresh token.',
invalid_id_token: 'Invalid id token.',
});

export type LogtoClientErrorCode = NormalizeKeyPaths<typeof logtoClientErrorCodes>;

const getMessageByErrorCode = (errorCode: LogtoClientErrorCode): string => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const message = get(logtoClientErrorCodes, errorCode);

if (typeof message === 'string') {
return message;
}

return errorCode;
};

export class LogtoClientError extends Error {
code: LogtoClientErrorCode;
data: unknown;

constructor(code: LogtoClientErrorCode, data?: unknown) {
super(getMessageByErrorCode(code));
this.code = code;
this.data = data;
}
}
Loading

0 comments on commit 04c7b56

Please sign in to comment.