Skip to content

Commit

Permalink
feat: add apiClient revocation registry
Browse files Browse the repository at this point in the history
  • Loading branch information
lotharking committed Dec 19, 2024
1 parent 25d4a97 commit 9131bc7
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 13 deletions.
8 changes: 6 additions & 2 deletions packages/client/src/ApiClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// src/ApiClient.ts

import { RevocationRegistryService } from './services'
import { CredentialTypeService } from './services/CredentialTypeService'
import { MessageService } from './services/MessageService'
import { ApiVersion } from './types/enums'
Expand All @@ -23,24 +24,27 @@ import { ApiVersion } from './types/enums'
* const apiClient = new ApiClient('http://localhost', ApiVersion.V1)
*
* // Example to query available credentials
* await apiClient.credentialType.getAllCredentialTypes()
* await apiClient.credentialType.getAll()
*
* // Example to send a message
* apiClient.message.sendMessage(message: BaseMessage)
* apiClient.message.send(message: BaseMessage)
*
* The `ApiClient` class provides easy methods for interacting with:
* - `message`: Send and manage messages.
* - `credentialType`: Query and manage credential types.
* - `revocationRegistry`: Query and manage the revocation registry for credential definitions.
*/
export class ApiClient {
public readonly messages: MessageService
public readonly credentialTypes: CredentialTypeService
public readonly revocationRegistry: RevocationRegistryService

constructor(
private baseURL: string,
private version: ApiVersion = ApiVersion.V1,
) {
this.messages = new MessageService(baseURL, version)
this.credentialTypes = new CredentialTypeService(baseURL, version)
this.revocationRegistry = new RevocationRegistryService(baseURL, version)
}
}
9 changes: 0 additions & 9 deletions packages/client/src/services/CredentialTypeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,4 @@ export class CredentialTypeService {

return types.map(value => value as CredentialTypeInfo)
}

public async createRevocation(credentialDefinitionId: string): Promise<string> {
const response = await fetch(`${this.url}/revoke`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ credentialDefinitionId }),
})
return response.text()
}
}
70 changes: 70 additions & 0 deletions packages/client/src/services/RevocationRegistryService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// src/services/RevocationRegistryService.ts

import { RevocationRegistryInfo } from '@2060.io/service-agent-model'
import { Logger } from 'tslog'

import { ApiVersion } from '../types/enums'

const logger = new Logger({
name: 'RevocationRegistryService',
type: 'pretty',
prettyLogTemplate: '{{logLevelName}} [{{name}}]: ',
})

/**
* `RevocationRegistryService` class for managing credential types and interacting with
* the available endpoints related to credential types in the Agent Service.
*
* This class provides methods for querying, creating, and managing revocation registry on credential types.
* For a list of available endpoints and functionality, refer to the methods within this class.
*/
export class RevocationRegistryService {
private url: string

constructor(
private baseURL: string,
private version: ApiVersion,
) {
this.url = `${this.baseURL.replace(/\/$/, '')}/${this.version}/credential-types`
}

public async create(options: RevocationRegistryInfo): Promise<string> {
logger.info(`Creating revocation registry with credentialDefinitionId: ${JSON.stringify(options)}`)
const response = await fetch(`${this.url}/revocationRegistry`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ options }),
})
return response.text()
}

public async get(credentialDefinitionId: string): Promise<string[]> {
logger.info(`Searching revocation registry with credentialDefinitionId: ${credentialDefinitionId}`)
const response = await fetch(`${this.url}/revocationRegistry`, {
method: 'GET',
headers: { accept: 'application/json', 'Content-Type': 'application/json' },
body: JSON.stringify({ credentialDefinitionId }),
})

if (!response.ok) {
throw new Error(`Failed to fetch revocation definitions: ${response.statusText}`)
}

return (await response.json()) as string[]
}

public async getAll(): Promise<string[]> {
logger.info(`Searching all revocation registry`)
const response = await fetch(`${this.url}/revocationRegistry`, {
method: 'GET',
headers: { accept: 'application/json', 'Content-Type': 'application/json' },
body: JSON.stringify({ credentialDefinitionId: '' }),
})

if (!response.ok) {
throw new Error(`Failed to fetch revocation definitions: ${response.statusText}`)
}

return (await response.json()) as string[]
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiProperty } from '@nestjs/swagger'
import { IsString, IsNotEmpty, IsNumber } from 'class-validator'
import { IsString, IsNotEmpty, IsNumber, IsOptional } from 'class-validator'

export class CreateRevocationRegistryDto {
@ApiProperty({
Expand All @@ -18,5 +18,6 @@ export class CreateRevocationRegistryDto {
})
@IsNumber()
@IsNotEmpty()
@IsOptional()
maximumCredentialNumber: number = 1000
}
2 changes: 1 addition & 1 deletion packages/model/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export interface CredentialTypeInfo extends CreateCredentialTypeOptions {
id: string
}

export interface RevocationRegistry {
export interface RevocationRegistryInfo {
credentialDefinitionId: string
maximumCredentialNumber: number
}
Expand Down

0 comments on commit 9131bc7

Please sign in to comment.