@logto/chrome-extension 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Silverhand
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # Logto Chrome extension SDK
2
+
3
+ [![Version](https://img.shields.io/npm/v/@logto/chrome-extension)](https://www.npmjs.com/package/@logto/browser)
4
+ [![Build Status](https://github.com/logto-io/js/actions/workflows/main.yml/badge.svg)](https://github.com/logto-io/js/actions/workflows/main.yml)
5
+
6
+ The Logto Chrome extension SDK written in TypeScript.
7
+
8
+ Check out our [docs](https://docs.logto.io/sdk/chrome-extension/) for more information; a sample project can be found at [here](../chrome-extension-sample/).
9
+
10
+ > [!Note]
11
+ > This package is ESM-only.
12
+
13
+ ## Installation
14
+
15
+ ### Using npm
16
+
17
+ ```bash
18
+ npm install @logto/chrome-extension
19
+ ```
20
+
21
+ ### Using yarn
22
+
23
+ ```bash
24
+ yarn add @logto/chrome-extension
25
+ ```
26
+
27
+ ### Using pnpm
28
+
29
+ ```bash
30
+ pnpm add @logto/chrome-extension
31
+ ```
32
+
33
+ ## Resources
34
+
35
+ [![Website](https://img.shields.io/badge/website-logto.io-8262F8.svg)](https://logto.io/)
36
+ [![Docs](https://img.shields.io/badge/docs-logto.io-green.svg)](https://docs.logto.io/)
37
+ [![Discord](https://img.shields.io/discord/965845662535147551?logo=discord&logoColor=ffffff&color=7389D8&cacheSeconds=600)](https://discord.gg/UEPaF3j5e6)
package/lib/index.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { BaseClient, type LogtoConfig } from '@logto/browser';
2
+ export * from '@logto/browser';
3
+ export { ChromeExtensionStorage } from './storage';
4
+ export default class LogtoClient extends BaseClient {
5
+ /**
6
+ * @param config The configuration object for the client.
7
+ */
8
+ constructor(config: LogtoConfig);
9
+ }
package/lib/index.js ADDED
@@ -0,0 +1,45 @@
1
+ import { BaseClient, LogtoClientError, createRequester, generateCodeChallenge, generateCodeVerifier, generateState, } from '@logto/browser';
2
+ import { ChromeExtensionStorage } from './storage';
3
+ export * from '@logto/browser';
4
+ export { ChromeExtensionStorage } from './storage';
5
+ export default class LogtoClient extends BaseClient {
6
+ /**
7
+ * @param config The configuration object for the client.
8
+ */
9
+ constructor(config) {
10
+ const requester = createRequester(fetch);
11
+ // eslint-disable-next-line unicorn/consistent-function-scoping -- we use `this` in the function
12
+ const navigate = async (url, params) => {
13
+ switch (params.for) {
14
+ case 'sign-in': {
15
+ const responseUrl = await chrome.identity.launchWebAuthFlow({ url, interactive: true });
16
+ if (!responseUrl) {
17
+ throw new LogtoClientError('user_cancelled');
18
+ }
19
+ await this.handleSignInCallback(responseUrl);
20
+ break;
21
+ }
22
+ case 'sign-out': {
23
+ await chrome.identity.launchWebAuthFlow({
24
+ url,
25
+ interactive: false,
26
+ abortOnLoadForNonInteractive: false,
27
+ timeoutMsForNonInteractive: 10_000,
28
+ });
29
+ break;
30
+ }
31
+ default: {
32
+ throw new Error(`Unsupported navigation for ${params.for}`);
33
+ }
34
+ }
35
+ };
36
+ super(config, {
37
+ requester,
38
+ navigate,
39
+ storage: new ChromeExtensionStorage(config.appId),
40
+ generateCodeChallenge,
41
+ generateCodeVerifier,
42
+ generateState,
43
+ });
44
+ }
45
+ }
@@ -0,0 +1,9 @@
1
+ import { type PersistKey, type Storage } from '@logto/browser';
2
+ export declare class ChromeExtensionStorage implements Storage<PersistKey> {
3
+ readonly appId: string;
4
+ constructor(appId: string);
5
+ getKey(item?: string): string;
6
+ getItem(key: PersistKey): Promise<string | null>;
7
+ setItem(key: PersistKey, value: string): Promise<void>;
8
+ removeItem(key: PersistKey): Promise<void>;
9
+ }
package/lib/storage.js ADDED
@@ -0,0 +1,24 @@
1
+ const keyPrefix = `logto`;
2
+ export class ChromeExtensionStorage {
3
+ constructor(appId) {
4
+ this.appId = appId;
5
+ }
6
+ getKey(item) {
7
+ if (item === undefined) {
8
+ return `${keyPrefix}:${this.appId}`;
9
+ }
10
+ return `${keyPrefix}:${this.appId}:${item}`;
11
+ }
12
+ // eslint-disable-next-line @typescript-eslint/ban-types
13
+ async getItem(key) {
14
+ const storageKey = this.getKey(key);
15
+ const object = await chrome.storage.local.get(storageKey);
16
+ return object[storageKey] ? String(object[storageKey]) : null;
17
+ }
18
+ async setItem(key, value) {
19
+ await chrome.storage.local.set({ [this.getKey(key)]: value });
20
+ }
21
+ async removeItem(key) {
22
+ await chrome.storage.local.remove(this.getKey(key));
23
+ }
24
+ }
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@logto/chrome-extension",
3
+ "version": "0.1.0",
4
+ "description": "Logto Chrome extension SDK",
5
+ "type": "module",
6
+ "files": [
7
+ "lib"
8
+ ],
9
+ "module": "./lib/index.js",
10
+ "types": "./lib/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "import": "./lib/index.js",
14
+ "default": "./lib/index.js",
15
+ "types": "./lib/index.d.ts"
16
+ }
17
+ },
18
+ "keywords": [],
19
+ "license": "MIT",
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "https://github.com/logto-io/js.git",
23
+ "directory": "packages/chrome-extension-sample"
24
+ },
25
+ "dependencies": {
26
+ "@logto/browser": "^2.2.6"
27
+ },
28
+ "devDependencies": {
29
+ "@silverhand/eslint-config": "^5.0.0",
30
+ "@silverhand/ts-config": "^5.0.0",
31
+ "chrome-types": "^0.1.276",
32
+ "eslint": "^8.57.0",
33
+ "lint-staged": "^15.0.0",
34
+ "prettier": "^3.0.0",
35
+ "typescript": "^5.3.3"
36
+ },
37
+ "eslintConfig": {
38
+ "extends": "@silverhand/react"
39
+ },
40
+ "prettier": "@silverhand/eslint-config/.prettierrc",
41
+ "publishConfig": {
42
+ "access": "public"
43
+ },
44
+ "scripts": {
45
+ "precommit": "lint-staged",
46
+ "build": "rm -rf lib && tsc",
47
+ "lint": "eslint --ext .ts --ext .tsx src"
48
+ }
49
+ }