Skip to content

Commit

Permalink
resolve discussions
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalii Smolinskyi committed Nov 19, 2024
1 parent 2209a08 commit ac9f570
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +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 |
| `getUserId` | extracts user 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.

Expand Down
12 changes: 9 additions & 3 deletions src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,16 @@ export function getProjectId(crowdinId: string): number {
/**
*
* @param crowdinId crowdin id (from {@link constructCrowdinIdFromJwtPayload})
* @returns crowdin user id
* @returns object with organization(id|domain), project id and user id
*/
export function getUserId(crowdinId: string): number {
return Number(crowdinId.split('__')[2]);
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]),
};
}

/**
Expand Down
8 changes: 5 additions & 3 deletions tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as jwt from 'jsonwebtoken';
import { constructCrowdinIdFromJwtPayload, getProjectId, getUserId, JwtPayload, validateJwtToken } from '../src';
import { constructCrowdinIdFromJwtPayload, getProjectId, parseCrowdinId, JwtPayload, validateJwtToken } from '../src';

describe('Token-based functions', () => {
const jwtPayload: JwtPayload = {
Expand Down Expand Up @@ -61,9 +61,11 @@ describe('Token-based functions', () => {
expect(projectId).toStrictEqual(jwtPayload.context.project_id);
});

it('getUserId', () => {
it('parseCrowdinId', () => {
const crowdinId = constructCrowdinIdFromJwtPayload(jwtPayload);
const userId = getUserId(crowdinId);
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);
});
});

0 comments on commit ac9f570

Please sign in to comment.