From 99c1036ea5b5f108c4ca99457c3aa373128bd533 Mon Sep 17 00:00:00 2001 From: Vitalii Smolinskyi Date: Thu, 28 Nov 2024 10:21:38 +0200 Subject: [PATCH] feat: add getUserId (#65) --- README.md | 41 +++++++++++++++++++++-------------------- src/token.ts | 15 +++++++++++++++ tests/index.spec.ts | 18 +++++++++++++----- 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index a82dc46..8cdd135 100644 --- a/README.md +++ b/README.md @@ -79,26 +79,27 @@ crowdinFunctions.generateOAuthToken({ clientId: 'app_client_id', clientSecret: ' Mainly all functions are intended to help you quickly develop your app but they also can reduce some work when you are integrating with Crowdin API. Please refer to JSDoc for more details. -| Method name | Description | -|------------------------------------|-----------------------------------------------------------------| -| `fetchAppToken` | fetch API token for communication with Crowdin API | -| `fetchAgentToken` | fetch Agent API token for communication with Crowdin API | -| `generateOAuthToken` | generates OAuth token for communication with Crowdin API | -| `refreshOAuthToken` | refresh OAuth token in case if it was expired | -| `constructCrowdinIdFromJwtPayload` | creates unique id of crowdin user and project from the context | -| `getProjectId` | extracts project id from crowdin id | -| `validateJwtToken` | validates if jwt token for your app is valid | -| `updateOrCreateFile` | create or update file in Crowdin | -| `getFolder` | get folder with and files under it | -| `getOrCreateFolder` | get folder with files under it or create it | -| `uploadTranslations` | adds file to storage and sends it in upload translation request | -| `updateSourceFiles` | updates source files under specific directory | -| `handleTranslations` | executes side effect function for each translated file | -| `createOrUpdateWebhook` | create or update webhook | -| `getSubscription` | returns an information about app subscription | -| `convertString` | converts source or translation according to specified config | -| `getBundleConfigurationForm` | returns UI template config for custom formatters | -| `generateReport` | generates Crowdin report, covers check if finished | +| Method name | Description | +|------------------------------------|-----------------------------------------------------------------------| +| `fetchAppToken` | fetch API token for communication with Crowdin API | +| `fetchAgentToken` | fetch Agent API token for communication with Crowdin API | +| `generateOAuthToken` | generates OAuth token for communication with Crowdin API | +| `refreshOAuthToken` | refresh OAuth token in case if it was expired | +| `constructCrowdinIdFromJwtPayload` | creates unique id of crowdin user and project from the context | +| `getProjectId` | extracts project id from crowdin id | +| `parseCrowdinId` | extracts object with organization(id\|domain), project id and user id | +| `validateJwtToken` | validates if jwt token for your app is valid | +| `updateOrCreateFile` | create or update file in Crowdin | +| `getFolder` | get folder with and files under it | +| `getOrCreateFolder` | get folder with files under it or create it | +| `uploadTranslations` | adds file to storage and sends it in upload translation request | +| `updateSourceFiles` | updates source files under specific directory | +| `handleTranslations` | executes side effect function for each translated file | +| `createOrUpdateWebhook` | create or update webhook | +| `getSubscription` | returns an information about app subscription | +| `convertString` | converts source or translation according to specified config | +| `getBundleConfigurationForm` | returns UI template config for custom formatters | +| `generateReport` | generates Crowdin report, covers check if finished | Also please have a look to working example of the [Crowdin App](https://github.com/crowdin/create-crowdin-app). It can be used as a basis for your app. diff --git a/src/token.ts b/src/token.ts index 7683634..bad5a9c 100644 --- a/src/token.ts +++ b/src/token.ts @@ -235,6 +235,21 @@ export function getProjectId(crowdinId: string): number { return Number(crowdinId.split('__')[1]); } +/** + * + * @param crowdinId crowdin id (from {@link constructCrowdinIdFromJwtPayload}) + * @returns object with organization(id|domain), project id and user id + */ +export function parseCrowdinId(crowdinId: string): { organization: string; projectId: number; userId: number } { + const crowdinIdParts = crowdinId.split('__'); + + return { + organization: crowdinIdParts[0], + projectId: Number(crowdinIdParts[1]), + userId: Number(crowdinIdParts[2]), + }; +} + /** * * @param jwtToken jwt token which Crowdin adds to app iframe diff --git a/tests/index.spec.ts b/tests/index.spec.ts index 98e2b9c..feffdfe 100644 --- a/tests/index.spec.ts +++ b/tests/index.spec.ts @@ -1,10 +1,10 @@ import * as jwt from 'jsonwebtoken'; -import { constructCrowdinIdFromJwtPayload, getProjectId, JwtPayload, validateJwtToken } from '../src'; +import { constructCrowdinIdFromJwtPayload, getProjectId, parseCrowdinId, JwtPayload, validateJwtToken } from '../src'; describe('Token-based functions', () => { const jwtPayload: JwtPayload = { aud: 'test', - sub: 'test', + sub: '3', iat: 1, exp: Math.floor(Date.now() / 1000) + 60 * 60, context: { @@ -17,18 +17,18 @@ describe('Token-based functions', () => { it('constructCrowdinIdFromJwtPayload', () => { const jwtPayload2: JwtPayload = { aud: 'test', - sub: 'testNew', + sub: '4', iat: 1, exp: 1, context: { organization_id: 1, project_id: 2, - user_id: 3, + user_id: 4, }, }; const jwtPayload3: JwtPayload = { aud: 'test', - sub: 'test', + sub: '3', iat: 1, exp: 1, context: { @@ -60,4 +60,12 @@ describe('Token-based functions', () => { const projectId = getProjectId(crowdinId); expect(projectId).toStrictEqual(jwtPayload.context.project_id); }); + + it('parseCrowdinId', () => { + const crowdinId = constructCrowdinIdFromJwtPayload(jwtPayload); + const { organization, projectId, userId } = parseCrowdinId(crowdinId); + expect(+organization).toStrictEqual(jwtPayload.context.organization_id); + expect(projectId).toStrictEqual(jwtPayload.context.project_id); + expect(userId).toStrictEqual(jwtPayload.context.user_id); + }); });