Skip to content

Commit

Permalink
fix docstrings
Browse files Browse the repository at this point in the history
Signed-off-by: Musilah <[email protected]>
  • Loading branch information
Musilah committed Apr 4, 2024
1 parent 0c9cdf3 commit 53481a6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 45 deletions.
8 changes: 4 additions & 4 deletions examples/certs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const mySdk = new SDK({
})

mySdk.certs
.Issue(
.IssueCert(
'<thingID>',
'<valid>',
'<token>'
Expand All @@ -20,7 +20,7 @@ mySdk.certs
})

mySdk.certs
.ViewByThing(
.ViewCertByThing(
'<thingID>',
'<token>'
)
Expand All @@ -32,7 +32,7 @@ mySdk.certs
})

mySdk.certs
.ViewBySerial(
.ViewCert(
'<certID>',
'<token>'
)
Expand All @@ -44,7 +44,7 @@ mySdk.certs
})

mySdk.certs
.Revoke(
.RevokeCert(
'<thingID>',
'<token>'
)
Expand Down
86 changes: 45 additions & 41 deletions src/certs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import Errors from './errors'
import {
type Cert,
type CertSerials,
type Response
} from './defs'

export default class Certs {
// Certs API Client
Expand All @@ -23,31 +28,29 @@ export default class Certs {
this.certsError = new Errors()
}

public async Issue (thingID: string, valid: string, token: string): Promise<any> {
public async IssueCert (thingID: string, valid: string, token: string): Promise<Cert> {
// Issue a certificate
/**
* @method Issue - Issue a certificate to a thing.
* Requires a thingID and a valid time in hours as well as a token.
* @param {string} thingID - The thingID of the thing to be issued a certificate.
* @param {string} valid - The time in hours for which the certificate is valid such as '10h'
* @example
* const certs = {
* "cert_serial": "22:16:df:60:c2:99:bc:c4:9b:1d:fd:71:5e:e9:07:d9:1b:3c:85:1d",
* "client_cert": "-----BEGIN CERTIFICATE-----\nMIIEATCCAumgAwIBAgIUIhbfYMKZvMSbHf1xXukH2Rs8hR0wDQYJKoZIhvcNAQEL1k\n-----END CERTIFICATE-----",
* "client_key": "-----BEGIN RSA PRIVATE KEY-----\nMIIEoQIBAAKCAQEAy9gF84a5s6jlX6hkAPXrLYqvdhe6uygdr6eHfd5erdcdxfgc\n-----END RSA PRIVATE KEY-----",
* "expiration": "2023-09-20T10:02:48Z",
* "thingID": "3d49a42f-63fd-491b-9784-adf4b64ef347"
* }
*/

const payload = { thingID, ttl: valid }
* @method IssueCert - Issue a certificate to a thing.
* Requires a thingID and a valid time in hours as well as a token.
* @param {string} thingID - The thingID of the thing to be issued a certificate.
* @param {string} valid - The time in hours for which the certificate is valid such as '10h'
* @example
* const certs = {
* "cert_serial": "22:16:df:60:c2:99:bc:c4:9b:1d:fd:71:5e:e9:07:d9:1b:3c:85:1d",
* "client_cert": "-----BEGIN CERTIFICATE-----\nMIIEATCCAumgAwIBAgIUIhbfYMKZvMSbHf1xXukH2Rs8hR0wDQYJKoZIhvcNAQEL1k\n-----END CERTIFICATE-----",
* "client_key": "-----BEGIN RSA PRIVATE KEY-----\nMIIEoQIBAAKCAQEAy9gF84a5s6jlX6hkAPXrLYqvdhe6uygdr6eHfd5erdcdxfgc\n-----END RSA PRIVATE KEY-----",
* "expiration": "2023-09-20T10:02:48Z",
* "thingID": "3d49a42f-63fd-491b-9784-adf4b64ef347"
* }
*/
const options: RequestInit = {
method: 'POST',
headers: {
'Content-Type': this.contentType,
Authorization: `Bearer ${token}`
},
body: JSON.stringify(payload)
body: JSON.stringify({ thing_id: thingID, ttl: valid })
}

try {
Expand All @@ -59,25 +62,25 @@ export default class Certs {
const errorRes = await response.json()
throw this.certsError.HandleError(errorRes.error, response.status)
}
const certsData = await response.json()
const certsData: Cert = await response.json()
return certsData
} catch (error) {
throw error
}
}

public async ViewByThing (thingID: string, token: string): Promise<any> {
public async ViewCertByThing (thingID: string, token: string): Promise<CertSerials> {
// View certificates by thingID
/**
* @method ViewByThing - Allows a logged in user to view a certificate serial once they
* provide a valid connected thing-id and token.
* @param {string} thingID - The thingID of the thing whose certificate is to be viewed.
* @param {string} token - The token to be used for authentication.
*
*/
* @method ViewCertByThing - Allows a logged in user to view a certificate serial once they
* provide a valid connected thing-id and token.
* @param {string} thingID - The thingID of the thing whose certificate is to be viewed.
* @param {string} token - The token to be used for authentication.
*
*/

const options: RequestInit = {
method: 'get',
method: 'GET',
headers: {
'Content-Type': this.contentType,
Authorization: `Bearer ${token}`
Expand All @@ -92,22 +95,22 @@ export default class Certs {
const errorRes = await response.json()
throw this.certsError.HandleError(errorRes.error, response.status)
}
const certsData = await response.json()
const certsData: CertSerials = await response.json()
return certsData
} catch (error) {
throw error
}
}

public async ViewBySerial (certID: string, token: string): Promise<any> {
public async ViewCert (id: string, token: string): Promise<Cert> {
// View certificate by certID
/**
* @method ViewBySerial - Allows a logged in user to view a certificate once they
* provide a valid cert-id and token.
* @param {string} certID - The certID of the certificate to be viewed.
* @param {string} token - The token to be used for authentication.
*
*/
* @method ViewCert - Allows a logged in user to view a certificate once they
* provide a valid cert-id and token.
* @param {string} id - The ID of the certificate to be viewed.
* @param {string} token - The token to be used for authentication.
*
*/

const options: RequestInit = {
method: 'GET',
Expand All @@ -119,26 +122,26 @@ export default class Certs {

try {
const response = await fetch(
new URL(`${this.certsEndpoint}/${certID}`, this.certsUrl).toString(),
new URL(`${this.certsEndpoint}/${id}`, this.certsUrl).toString(),
options
)
if (!response.ok) {
const errorRes = await response.json()
throw this.certsError.HandleError(errorRes.error, response.status)
}
const certsData = await response.json()
const certsData: Cert = await response.json()
return certsData
} catch (error) {
throw error
}
}

public async Revoke (thingID: string, token: string): Promise<any> {
public async RevokeCert (id: string, token: string): Promise<Response> {
// Revoke a certificate
/**
* @method Revoke - Allows a logged in user to delete a certificate once they
* @method RevokeCert - Allows a logged in user to delete a certificate once they
* provide a valid thing-id and token.
* @param {string} thingID - The thingID of the certificate to be revoked.
* @param {string} id - The id of the certificate to be revoked.
* @param {string} token - The token to be used for authentication.
*/

Expand All @@ -152,14 +155,15 @@ export default class Certs {

try {
const response = await fetch(
new URL(`${this.certsEndpoint}/${thingID}`, this.certsUrl).toString(),
new URL(`${this.certsEndpoint}/${id}`, this.certsUrl).toString(),
options
)
if (!response.ok) {
const errorRes = await response.json()
throw this.certsError.HandleError(errorRes.error, response.status)
}
return 'DELETED'
const revokeResponse: Response = { status: response.status, message: 'Cert Revoked Successfully' }
return revokeResponse
} catch (error) {
throw error
}
Expand Down
13 changes: 13 additions & 0 deletions src/defs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,16 @@ export interface DomainsPage {
export interface Permissions {
permissions: string[]
}

export interface Cert {
thing_id?: string
cert_serial?: string
client_key?: string
client_cert?: string
expiration?: Date
}

export interface CertSerials {
certs: Cert[]
page: PageRes
}

0 comments on commit 53481a6

Please sign in to comment.