Skip to content

Commit

Permalink
feat: common package (#111)
Browse files Browse the repository at this point in the history
* feat: common package
  • Loading branch information
carlos-affinidi authored Jun 27, 2024
1 parent 576292c commit 0293729
Show file tree
Hide file tree
Showing 13 changed files with 6,771 additions and 82 deletions.
1,433 changes: 1,351 additions & 82 deletions package-lock.json

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions packages/common/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"root": true,
"ignorePatterns": [
"*.js",
"*.d.ts",
"node_modules/"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2021,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended" // Enables eslint-plugin-prettier and eslint-config-prettier
],
"rules": {
"@typescript-eslint/no-explicit-any": "off"
}
}
34 changes: 34 additions & 0 deletions packages/common/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
tsconfig.json
.jsii

*.js
*.d.ts

### Node ###
# Logs
logs
*.log
npm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Dependency directories
node_modules/

# TypeScript cache
*.tsbuildinfo

# Optional eslint cache
.eslintcache

# default build output
dist/

# Temporary folders
tmp/
temp/

# Custom
.idea/
.vscode/
18 changes: 18 additions & 0 deletions packages/common/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# empty npmignore to ensure all required files (e.g., in the dist folder) are published by npm

node_modules
coverage
.vscode
.DS_Store
.nyc_output
.history
.idea
Thumbs.db


# Exclude jsii outdir
dist

# Include .jsii and .jsii.gz
!.jsii
!.jsii.gz
20 changes: 20 additions & 0 deletions packages/common/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## @affinidi-tdk/common

### Build JSII

```
npm i --prefix .
npm run build
npm run package
```

The code will be generated under /dist for each language.

### Usage

### JS

```bash
npm install @affinidi-tdk/common
```

65 changes: 65 additions & 0 deletions packages/common/helpers/environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
export enum Environment {
LOCAL = 'local',
DEVELOPMENT = 'dev',
PRODUCTION = 'prod',
}

const DEFAULT_REGION = 'ap-southeast-1'

const envToApiGwUrl = {
[Environment.LOCAL]: 'https://apse1.dev.api.affinidi.io',
[Environment.DEVELOPMENT]: 'https://apse1.dev.api.affinidi.io',
[Environment.PRODUCTION]: 'https://apse1.api.affinidi.io',
}

const envToElementsAuthTokenUrl = {
[Environment.LOCAL]:
'https://apse1.dev.auth.developer.affinidi.io/auth/oauth2/token',
[Environment.DEVELOPMENT]:
'https://apse1.dev.auth.developer.affinidi.io/auth/oauth2/token',
[Environment.PRODUCTION]:
'https://apse1.auth.developer.affinidi.io/auth/oauth2/token',
}

const envToIotUrl = {
[Environment.LOCAL]: 'a3sq1vuw0cw9an-ats.iot.ap-southeast-1.amazonaws.com',
[Environment.DEVELOPMENT]:
'a3sq1vuw0cw9an-ats.iot.ap-southeast-1.amazonaws.com',
[Environment.PRODUCTION]:
'a3sq1vuw0cw9an-ats.iot.ap-southeast-1.amazonaws.com',
}

export class EnvironmentUtils {
static fetchEnvironment(): Environment {
const backendEnv = process.env.AFFINIDI_TDK_ENVIRONMENT
if (backendEnv) {
return backendEnv as Environment
}

const nextPublicEnv = process.env.NEXT_PUBLIC_AFFINIDI_TDK_ENVIRONMENT
if (nextPublicEnv) {
return nextPublicEnv as Environment
}

return Environment.PRODUCTION
}

static fetchApiGwUrl(): string {
const env = this.fetchEnvironment()
return `${envToApiGwUrl[env]}`
}

static fetchElementsAuthTokenUrl(): string {
const env = this.fetchEnvironment()
return `${envToElementsAuthTokenUrl[env]}`
}

static fetchRegion(): string {
return DEFAULT_REGION
}

static fetchIotUrl(): string {
const env = this.fetchEnvironment()
return `${envToIotUrl[env]}`
}
}
3 changes: 3 additions & 0 deletions packages/common/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { Environment, EnvironmentUtils } from './environment'
export { VaultUtils } from './vault'
export { Logger } from './logger'
12 changes: 12 additions & 0 deletions packages/common/helpers/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Environment, EnvironmentUtils } from './environment'

export class Logger {
private static isDebugEnabled: boolean =
EnvironmentUtils.fetchEnvironment() !== Environment.PRODUCTION

static debug(message: string, ...optionalParams: any[]): void {
if (this.isDebugEnabled) {
console.log(`TDK: ${message}`, ...optionalParams)
}
}
}
33 changes: 33 additions & 0 deletions packages/common/helpers/vault.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Environment, EnvironmentUtils } from './environment'

const LOCAL_VAULT_URL = 'http://localhost:3001'
const DEV_VAULT_URL = 'https://vault.dev.affinidi.com'
const PROD_VAULT_URL = 'https://vault.affinidi.com'

const envToWebVaultUrlMap = {
[Environment.LOCAL]: LOCAL_VAULT_URL,
[Environment.DEVELOPMENT]: DEV_VAULT_URL,
[Environment.PRODUCTION]: PROD_VAULT_URL,
}

const SHARE_PATH = '/login'
const CLAIM_PATH = '/claim'

export class VaultUtils {
static buildShareLink(request: string, client_id: string): string {
const env = EnvironmentUtils.fetchEnvironment()
const params = new URLSearchParams()
params.append('request', request)
params.append('client_id', client_id)
const queryString = params.toString()
return `${envToWebVaultUrlMap[env] || PROD_VAULT_URL}${SHARE_PATH}?${queryString}`
}

static buildClaimLink(credentialOfferUri: string): string {
const env = EnvironmentUtils.fetchEnvironment()
const params = new URLSearchParams()
params.append('credential_offer_uri', credentialOfferUri)
const queryString = params.toString()
return `${envToWebVaultUrlMap[env] || PROD_VAULT_URL}${CLAIM_PATH}?${queryString}`
}
}
1 change: 1 addition & 0 deletions packages/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Environment, EnvironmentUtils, VaultUtils } from './helpers'
Loading

0 comments on commit 0293729

Please sign in to comment.