@goiam/typescript
Installation
Bash
npm install @goiam/typescript
# or
pnpm add @goiam/typescript
# or
yarn add @goiam/typescript
Usage
Initialize the SDK
Typescript
import { GoIamSdk } from '@goiam/typescript';
const sdk = new GoIamSdk('https://go-iam.example.com', 'your-client-id', 'your-secret');
Verify Authentication Code
Typescript
try {
const token = await sdk.verify('auth-code');
console.log('Access Token:', token);
} catch (error) {
console.error('Failed to verify code:', error);
}
Fetch Current User Information
Typescript
try {
const user = await sdk.me(token);
console.log('User:', user);
} catch (error) {
console.error('Failed to fetch user information:', error);
}
Create a Resource
Typescript
const resource = {
id: 'resource-id',
name: 'Resource Name',
description: 'Resource Description',
key: 'resource-key',
enabled: true,
projectId: 'project-id',
createdBy: 'creator',
updatedBy: 'updater',
};
try {
await sdk.createResource(resource, token);
console.log('Resource created successfully');
} catch (error) {
console.error('Failed to create resource:', error);
}
Types
User
Typescript
interface User {
id: string;
projectId: string;
name: string;
email: string;
phone: string;
enabled: boolean;
profile_pic: string;
expiry?: string;
roles: Record<string, UserRole>;
resources: Record<string, UserResource>;
policies: Record<string, UserPolicy>;
created_at?: string;
created_by: string;
updated_at?: string;
updated_by: string;
}
interface UserPolicy {
name: string;
mapping: UserPolicyMapping;
}
interface UserPolicyMapping {
arguments: Record<string, UserPolicyMappingValue>;
}
interface UserPolicyMappingValue {
static: string;
}
interface UserRole {
id: string;
name: string;
}
interface UserResource {
role_ids: { [key: string]: boolean };
policy_ids: { [key: string]: boolean };
key: string;
name: string;
}
Resource
Typescript
interface Resource {
id: string;
name: string;
description: string;
key: string;
enabled: boolean;
projectId: string;
createdAt?: string;
createdBy: string;
updatedAt?: string;
updatedBy: string;
deletedAt?: string;
}
Error Handling
Typescript
try {
const result = await sdk.verify('code');
// Handle success
} catch (error) {
console.error('API Error:', error.message);
// Handle error
}