Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add getUserId #65

Merged
merged 4 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@crowdin/crowdin-apps-functions",
"version": "0.9.1",
"version": "0.9.2",
"description": "Utility library to easily and quickly develop Crowdin App",
"main": "out/index.js",
"types": "out/index.d.ts",
Expand Down
15 changes: 15 additions & 0 deletions src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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: {
Expand Down Expand Up @@ -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);
});
});
Loading