@nicheprotocol/darwin-sdk
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -8,3 +8,3 @@ module.exports = { | ||
], | ||
plugins: ['@typescript-eslint', 'react', 'prettier', 'import', 'jest'], | ||
plugins: ['@typescript-eslint', 'prettier', 'import', 'jest'], | ||
env: { | ||
@@ -20,8 +20,2 @@ 'jest/globals': true, | ||
'import/no-unresolved': 0, | ||
'react/jsx-filename-extension': [ | ||
1, | ||
{ | ||
extensions: ['.ts', '.tsx'], | ||
}, | ||
], | ||
'prettier/prettier': [ | ||
@@ -38,3 +32,2 @@ 'error', | ||
'import/extensions': 0, | ||
'react/prop-types': 0, | ||
'no-shadow': 'off', | ||
@@ -47,3 +40,2 @@ '@typescript-eslint/no-shadow': ['error'], | ||
'@typescript-eslint/no-use-before-define': 0, | ||
'react/style-prop-object': 0, | ||
'class-methods-use-this': 0, | ||
@@ -50,0 +42,0 @@ 'no-underscore-dangle': 0, |
@@ -24,3 +24,2 @@ "use strict"; | ||
endpointUrl: `http://localhost:3000`, | ||
fetchTokenRoute: '/dev/gen_token', | ||
storage: window.localStorage, | ||
@@ -30,8 +29,7 @@ }); | ||
it('/accounts', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const api = yield (0, connect_1.connect)({ | ||
___dev_only: { | ||
id: '1234', | ||
fullName: 'Test', | ||
}, | ||
const token = yield (0, connect_1.getDevOnlyToken)('/dev/gen_token', { | ||
id: '1234', | ||
fullName: 'Test', | ||
}); | ||
const api = yield (0, connect_1.connect)({ token }); | ||
const accounts = yield api.accounts(); | ||
@@ -38,0 +36,0 @@ (0, testUtils_1.assertAPISuccess)(accounts); |
@@ -23,3 +23,2 @@ "use strict"; | ||
endpointUrl: `http://localhost:3000`, | ||
fetchTokenRoute: '/dev/gen_token', | ||
storage: window.localStorage, | ||
@@ -29,11 +28,11 @@ }); | ||
it('should set the token in localStorage', () => __awaiter(void 0, void 0, void 0, function* () { | ||
yield (0, connect_1.connect)({ | ||
___dev_only: { | ||
id: '1234', | ||
fullName: 'Test', | ||
}, | ||
const token = yield (0, connect_1.getDevOnlyToken)('/dev/gen_token', { | ||
id: '1234', | ||
fullName: 'Test', | ||
}); | ||
const token = window.localStorage.getItem(connect_1.TOKEN_STORAGE_KEY); | ||
expect(token).toBeDefined(); | ||
const api = yield (0, connect_1.connect)({ token }); | ||
expect(api).toBeDefined(); | ||
const storedToken = window.localStorage.getItem(connect_1.TOKEN_STORAGE_KEY); | ||
expect(storedToken).toBeDefined(); | ||
})); | ||
}); |
import { APIResponse, Success } from '../api'; | ||
export declare function assertAPISuccess<T extends any>(res: APIResponse<T>): asserts res is Success<T>; | ||
export declare function assertAPISuccess<T>(res: APIResponse<T>): asserts res is Success<T>; |
import { DarwinAPI } from './api'; | ||
export declare const TOKEN_STORAGE_KEY = "___darwin_jwt_token___"; | ||
export declare const connect: (options?: { | ||
___dev_only?: { | ||
id: string; | ||
fullName: string; | ||
authorizations?: string[]; | ||
}; | ||
export declare const getDevOnlyToken: (fetchTokenRoute: string, payload: { | ||
id: string; | ||
fullName: string; | ||
authorizations?: string[]; | ||
}) => Promise<string>; | ||
export declare const connect: ({ token, }: { | ||
token: string; | ||
}) => Promise<DarwinAPI>; |
@@ -12,9 +12,8 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.connect = exports.TOKEN_STORAGE_KEY = void 0; | ||
exports.connect = exports.getDevOnlyToken = exports.TOKEN_STORAGE_KEY = void 0; | ||
const api_1 = require("./api"); | ||
const store_1 = require("./store"); | ||
exports.TOKEN_STORAGE_KEY = '___darwin_jwt_token___'; | ||
const connect = (options) => __awaiter(void 0, void 0, void 0, function* () { | ||
var _a, _b, _c; | ||
const { endpointUrl, fetchTokenRoute, storage } = (0, store_1.get)(); | ||
const getDevOnlyToken = (fetchTokenRoute, payload) => __awaiter(void 0, void 0, void 0, function* () { | ||
const { endpointUrl, storage } = (0, store_1.get)(); | ||
if (!endpointUrl) { | ||
@@ -31,5 +30,5 @@ throw new Error(`Darwin not initialized with endpointUrl.`); | ||
body: JSON.stringify({ | ||
id: (_a = options === null || options === void 0 ? void 0 : options.___dev_only) === null || _a === void 0 ? void 0 : _a.id, | ||
fullName: (_b = options === null || options === void 0 ? void 0 : options.___dev_only) === null || _b === void 0 ? void 0 : _b.fullName, | ||
authorizations: (_c = options === null || options === void 0 ? void 0 : options.___dev_only) === null || _c === void 0 ? void 0 : _c.authorizations, | ||
id: payload.id, | ||
fullName: payload.fullName, | ||
authorizations: payload.authorizations, | ||
}), | ||
@@ -41,5 +40,29 @@ }); | ||
} | ||
storage.setItem(exports.TOKEN_STORAGE_KEY, token.data); | ||
return token.data; | ||
}); | ||
exports.getDevOnlyToken = getDevOnlyToken; | ||
const connect = ({ token, }) => __awaiter(void 0, void 0, void 0, function* () { | ||
if (!token.length) { | ||
throw new Error(`Invalid token!`); | ||
} | ||
const { endpointUrl, storage } = (0, store_1.get)(); | ||
if (!endpointUrl) { | ||
throw new Error(`Darwin not initialized with endpointUrl.`); | ||
} | ||
if (!storage) { | ||
throw new Error(`Expected storage strategy to be defined.`); | ||
} | ||
const res = yield fetch(`${endpointUrl}/verify`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}); | ||
if (res.status !== 200) { | ||
throw new Error(`Invalid token!`); | ||
} | ||
storage.setItem(exports.TOKEN_STORAGE_KEY, token); | ||
return new api_1.DarwinAPIImpl(); | ||
}); | ||
exports.connect = connect; |
export declare const server: { | ||
init: ({ endpointUrl, fetchTokenRoute, key, storage }: { | ||
init: ({ endpointUrl, key, storage }: { | ||
endpointUrl?: string | undefined; | ||
fetchTokenRoute?: string | undefined; | ||
key?: { | ||
@@ -6,0 +5,0 @@ publicKey: string; |
@@ -7,3 +7,2 @@ type TStorage = { | ||
endpointUrl?: string; | ||
fetchTokenRoute?: string; | ||
key?: { | ||
@@ -15,3 +14,3 @@ publicKey: string; | ||
}; | ||
export declare const init: ({ endpointUrl, fetchTokenRoute, key, storage }: DarwinState) => void; | ||
export declare const init: ({ endpointUrl, key, storage }: DarwinState) => void; | ||
export declare const deinit: () => void; | ||
@@ -18,0 +17,0 @@ export declare const get: () => DarwinState; |
@@ -16,11 +16,7 @@ "use strict"; | ||
let url = ''; | ||
let tokenRoute = ''; | ||
let storageStrategy; | ||
const init = ({ endpointUrl, fetchTokenRoute, key, storage }) => { | ||
const init = ({ endpointUrl, key, storage }) => { | ||
if (endpointUrl) { | ||
url = endpointUrl; | ||
} | ||
if (fetchTokenRoute) { | ||
tokenRoute = fetchTokenRoute; | ||
} | ||
if (key) { | ||
@@ -37,3 +33,2 @@ (0, secret_1.init)(key.publicKey, key.secretKey); | ||
endpointUrl: url, | ||
fetchTokenRoute: tokenRoute, | ||
key: (0, secret_1.get)(), | ||
@@ -40,0 +35,0 @@ storage: storageStrategy, |
@@ -5,2 +5,2 @@ /** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
testEnvironment: 'node', | ||
}; | ||
}; |
{ | ||
"name": "@nicheprotocol/darwin-sdk", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"main": "./dist/index.js", | ||
@@ -16,4 +16,12 @@ "types": "./dist/index.d.ts", | ||
"@types/jsonwebtoken": "^9.0.1", | ||
"@typescript-eslint/eslint-plugin": "^5.51.0", | ||
"@typescript-eslint/parser": "^5.51.0", | ||
"eslint": "^8.33.0", | ||
"eslint-config-prettier": "^8.6.0", | ||
"eslint-plugin-import": "^2.27.5", | ||
"eslint-plugin-jest": "^27.2.1", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"jest": "^29.4.2", | ||
"jest-environment-jsdom": "^29.4.2", | ||
"prettier": "^2.8.4", | ||
"ts-jest": "^29.0.5", | ||
@@ -20,0 +28,0 @@ "typescript": "^4.9.5", |
@@ -5,3 +5,3 @@ /** | ||
import { connect } from '../connect'; | ||
import { connect, getDevOnlyToken } from '../connect'; | ||
import { init } from '../store'; | ||
@@ -18,3 +18,2 @@ // Polyfill fetch. | ||
endpointUrl: `http://localhost:3000`, | ||
fetchTokenRoute: '/dev/gen_token', | ||
storage: window.localStorage, | ||
@@ -25,8 +24,7 @@ }); | ||
it('/accounts', async () => { | ||
const api = await connect({ | ||
___dev_only: { | ||
id: '1234', | ||
fullName: 'Test', | ||
}, | ||
const token = await getDevOnlyToken('/dev/gen_token', { | ||
id: '1234', | ||
fullName: 'Test', | ||
}); | ||
const api = await connect({ token }); | ||
@@ -33,0 +31,0 @@ const accounts: APIResponse<TAccount[]> = await api.accounts(); |
@@ -5,3 +5,3 @@ /** | ||
import { connect, TOKEN_STORAGE_KEY } from '../connect'; | ||
import { connect, getDevOnlyToken, TOKEN_STORAGE_KEY } from '../connect'; | ||
import { init } from '../store'; | ||
@@ -15,3 +15,2 @@ // Polyfill fetch. | ||
endpointUrl: `http://localhost:3000`, | ||
fetchTokenRoute: '/dev/gen_token', | ||
storage: window.localStorage, | ||
@@ -22,12 +21,12 @@ }); | ||
it('should set the token in localStorage', async () => { | ||
await connect({ | ||
___dev_only: { | ||
id: '1234', | ||
fullName: 'Test', | ||
}, | ||
const token = await getDevOnlyToken('/dev/gen_token', { | ||
id: '1234', | ||
fullName: 'Test', | ||
}); | ||
const api = await connect({ token }); | ||
expect(api).toBeDefined(); | ||
const token = window.localStorage.getItem(TOKEN_STORAGE_KEY); | ||
expect(token).toBeDefined(); | ||
const storedToken = window.localStorage.getItem(TOKEN_STORAGE_KEY); | ||
expect(storedToken).toBeDefined(); | ||
}); | ||
}); |
import { APIResponse, Success } from '../api'; | ||
// TypeScript assertion functions can not use arrow functions. | ||
export function assertAPISuccess<T extends any>( | ||
export function assertAPISuccess<T>( | ||
res: APIResponse<T>, | ||
@@ -6,0 +6,0 @@ ): asserts res is Success<T> { |
@@ -45,4 +45,6 @@ import { deinit, init } from '../secret'; | ||
const decodedToken = verifyToken(token); | ||
expect(Object.keys(decodedToken)).toEqual(expect.arrayContaining(['id', 'fullName', 'authorizations', 'iat'])); | ||
expect(Object.keys(decodedToken)).toEqual( | ||
expect.arrayContaining(['id', 'fullName', 'authorizations', 'iat']), | ||
); | ||
}); | ||
}); |
@@ -6,10 +6,10 @@ import { DarwinAPI, DarwinAPIImpl } from './api'; | ||
export const connect = async (options?: { | ||
___dev_only?: { id: string; fullName: string; authorizations?: string[] }; | ||
}): Promise<DarwinAPI> => { | ||
const { endpointUrl, fetchTokenRoute, storage } = get(); | ||
export const getDevOnlyToken = async ( | ||
fetchTokenRoute: string, | ||
payload: { id: string; fullName: string; authorizations?: string[] }, | ||
): Promise<string> => { | ||
const { endpointUrl, storage } = get(); | ||
if (!endpointUrl) { | ||
throw new Error(`Darwin not initialized with endpointUrl.`); | ||
} | ||
if (!storage) { | ||
@@ -24,5 +24,5 @@ throw new Error(`Expected storage strategy to be defined.`); | ||
body: JSON.stringify({ | ||
id: options?.___dev_only?.id, | ||
fullName: options?.___dev_only?.fullName, | ||
authorizations: options?.___dev_only?.authorizations, | ||
id: payload.id, | ||
fullName: payload.fullName, | ||
authorizations: payload.authorizations, | ||
}), | ||
@@ -36,5 +36,36 @@ }); | ||
storage.setItem(TOKEN_STORAGE_KEY, token.data); | ||
return token.data; | ||
}; | ||
export const connect = async ({ | ||
token, | ||
}: { | ||
token: string; | ||
}): Promise<DarwinAPI> => { | ||
if (!token.length) { | ||
throw new Error(`Invalid token!`); | ||
} | ||
const { endpointUrl, storage } = get(); | ||
if (!endpointUrl) { | ||
throw new Error(`Darwin not initialized with endpointUrl.`); | ||
} | ||
if (!storage) { | ||
throw new Error(`Expected storage strategy to be defined.`); | ||
} | ||
const res = await fetch(`${endpointUrl}/verify`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}); | ||
if (res.status !== 200) { | ||
throw new Error(`Invalid token!`); | ||
} | ||
storage.setItem(TOKEN_STORAGE_KEY, token); | ||
return new DarwinAPIImpl(); | ||
}; |
@@ -11,3 +11,2 @@ import { TOKEN_STORAGE_KEY } from './connect'; | ||
endpointUrl?: string; | ||
fetchTokenRoute?: string; | ||
key?: { | ||
@@ -21,12 +20,9 @@ publicKey: string; | ||
let url = ''; | ||
let tokenRoute = ''; | ||
let storageStrategy: TStorage; | ||
export const init = ({ endpointUrl, fetchTokenRoute, key, storage }: DarwinState) => { | ||
export const init = ({ endpointUrl, key, storage }: DarwinState) => { | ||
if (endpointUrl) { | ||
url = endpointUrl; | ||
} | ||
if (fetchTokenRoute) { | ||
tokenRoute = fetchTokenRoute; | ||
} | ||
if (key) { | ||
@@ -44,3 +40,2 @@ initKey(key.publicKey, key.secretKey); | ||
endpointUrl: url, | ||
fetchTokenRoute: tokenRoute, | ||
key: getKey(), | ||
@@ -47,0 +42,0 @@ storage: storageStrategy, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29517
38
915
15
3