@aptly-as/app-sdk
Advanced tools
Comparing version 0.2.5 to 0.3.0
@@ -14,3 +14,3 @@ import { AptlyApi } from './aptly/AptlyApi.js'; | ||
fetch(path, init) { | ||
return this.api.fetch(path, init); | ||
return this.api.fetchJSON(path, init); | ||
} | ||
@@ -17,0 +17,0 @@ set accessToken(token) { |
@@ -0,3 +1,4 @@ | ||
/// <reference types="node" /> | ||
import { AptlyAppJWT } from '@aptly-as/types'; | ||
import { RequestInit } from 'node-fetch'; | ||
import { RequestInit, Response } from 'node-fetch'; | ||
export interface AptlyApiOAuth2Response { | ||
@@ -21,3 +22,10 @@ access_token: string; | ||
post<T>(path: string, body: object, init?: Omit<RequestInit, 'body' | 'method'>): Promise<T>; | ||
fetch<T>(path: string, init?: RequestInit): Promise<T>; | ||
delete<T>(path: string, init?: Omit<RequestInit, 'body' | 'method'>): Promise<T>; | ||
downloadDocument(urlOrToken: string): Promise<{ | ||
buffer: Buffer; | ||
filename: string; | ||
contentType: string; | ||
}>; | ||
fetchJSON<T>(path: string, init?: RequestInit): Promise<T>; | ||
fetch(path: string, init?: RequestInit): Promise<Response>; | ||
getAccessToken(refresh_token?: string): Promise<AptlyAppJWT>; | ||
@@ -24,0 +32,0 @@ private basicAuth; |
@@ -23,6 +23,6 @@ import { AptlyApiError, AptlyErrorCode } from '@aptly-as/types'; | ||
async get(path, init) { | ||
return this.fetch(path, init); | ||
return this.fetchJSON(path, init); | ||
} | ||
async post(path, body, init) { | ||
return this.fetch(path, { | ||
return this.fetchJSON(path, { | ||
...init, | ||
@@ -33,9 +33,76 @@ method: 'POST', | ||
} | ||
async delete(path, init) { | ||
return this.fetchJSON(path, { | ||
...init, | ||
method: 'DELETE', | ||
}); | ||
} | ||
async downloadDocument(urlOrToken) { | ||
try { | ||
const documentUrl = urlOrToken.startsWith('http') ? urlOrToken : `${this.url}/documents/${urlOrToken}`; | ||
const response = await fetch(documentUrl); | ||
if (response.status !== 200) { | ||
throw new AptlyApiError({ | ||
status: String(response.status), | ||
response, | ||
title: response.statusText, | ||
code: AptlyErrorCode.Default, | ||
id: 'none', | ||
}); | ||
} | ||
let filename = ''; | ||
let contentType = ''; | ||
const regExpContentDispositionFilename = /filename="(?<filename>.*)"/; | ||
const regExpContentType = /Content-Type:\s(?<type>.*);/; | ||
const contentDispositionHeader = response.headers.get('Content-Disposition'); | ||
const contentTypeHeader = response.headers.get('Content-Type'); | ||
if (contentDispositionHeader) { | ||
filename = regExpContentDispositionFilename.exec(contentDispositionHeader)?.groups?.filename || ''; | ||
} | ||
if (contentTypeHeader) { | ||
contentType = regExpContentType.exec(contentTypeHeader)?.groups?.type || ''; | ||
} | ||
return { | ||
filename, | ||
contentType, | ||
buffer: Buffer.from(await response.arrayBuffer()), | ||
}; | ||
} | ||
catch (e) { | ||
if (e instanceof AptlyApiError) { | ||
throw e; | ||
} | ||
throw new AptlyApiError({ | ||
status: '500', | ||
title: 'Failed to parse file buffer', | ||
code: AptlyErrorCode.Default, | ||
id: 'none', | ||
}); | ||
} | ||
} | ||
async fetchJSON(path, init) { | ||
const response = await this.fetch(path, init); | ||
try { | ||
return (await response.json()); | ||
} | ||
catch (error) { | ||
throw new AptlyApiError({ | ||
status: String(response?.status || 500), | ||
detail: response.statusText, | ||
code: AptlyErrorCode.Default, | ||
link: '', | ||
title: String(response.status), | ||
id: 'none', | ||
error, | ||
response: response | ||
}); | ||
} | ||
} | ||
async fetch(path, init) { | ||
let response = null; | ||
while (this.busy) { | ||
await sleep(100); | ||
} | ||
this.busy = true; | ||
try { | ||
while (this.busy) { | ||
await sleep(100); | ||
} | ||
this.busy = true; | ||
if (!this.validateJWT()) { | ||
@@ -59,3 +126,3 @@ await this.getAccessToken(); | ||
code: AptlyErrorCode.Default, | ||
detail: 'Failed to parse json on error', | ||
detail: String(response.status), | ||
id: 'none', | ||
@@ -66,17 +133,3 @@ error, | ||
} | ||
try { | ||
return (await response.json()); | ||
} | ||
catch (error) { | ||
throw new AptlyApiError({ | ||
status: String(response?.status || 500), | ||
detail: 'Failed to parse json', | ||
code: AptlyErrorCode.Default, | ||
link: '', | ||
title: 'Failed to parse json', | ||
id: 'none', | ||
error, | ||
response: response | ||
}); | ||
} | ||
return response; | ||
} | ||
@@ -83,0 +136,0 @@ catch (error) { |
@@ -6,2 +6,3 @@ import { AptlyApi } from './AptlyApi.js'; | ||
import { AptlyProjects } from './AptlyProjects.js'; | ||
import { AptlyWebhooks } from './AptlyWebhooks.js'; | ||
export declare class AptlyOrganization { | ||
@@ -16,5 +17,7 @@ private api; | ||
units(): AptlyUnits; | ||
webhooks(): AptlyWebhooks; | ||
project(projectID: string): AptlyProjects; | ||
unit(unitID: string): AptlyUnits; | ||
webhook(webhookID: string): AptlyWebhooks; | ||
get(): Promise<unknown>; | ||
} |
@@ -5,2 +5,3 @@ import { AptlyProducts } from './AptlyProducts.js'; | ||
import { AptlyProjects } from './AptlyProjects.js'; | ||
import { AptlyWebhooks } from './AptlyWebhooks.js'; | ||
export class AptlyOrganization { | ||
@@ -24,2 +25,5 @@ constructor(api, path, id) { | ||
} | ||
webhooks() { | ||
return new AptlyWebhooks(this.api, `${this.path}/${this.id}`); | ||
} | ||
project(projectID) { | ||
@@ -31,2 +35,5 @@ return new AptlyProjects(this.api, `${this.path}/${this.id}`, projectID); | ||
} | ||
webhook(webhookID) { | ||
return new AptlyWebhooks(this.api, `${this.path}/${this.id}`, webhookID); | ||
} | ||
get() { | ||
@@ -33,0 +40,0 @@ return this.api.get(this.path); |
@@ -6,2 +6,3 @@ import { AptlyApiQueries, AptlyProject, AptlySearchPaginateResponse } from '@aptly-as/types'; | ||
import { AptlyUnits } from './AptlyUnits.js'; | ||
import { AptlyWebhooks } from './AptlyWebhooks.js'; | ||
export declare class AptlyProjects { | ||
@@ -16,2 +17,4 @@ private api; | ||
documents(): AptlyDocuments; | ||
webhooks(): AptlyWebhooks; | ||
webhook(webhookId: string): AptlyWebhooks; | ||
get(query: { | ||
@@ -18,0 +21,0 @@ [key in keyof AptlyProject]?: any; |
import { AptlyDocuments } from './AptlyDocuments.js'; | ||
import { AptlyProducts } from './AptlyProducts.js'; | ||
import { AptlyUnits } from './AptlyUnits.js'; | ||
import { AptlyWebhooks } from './AptlyWebhooks.js'; | ||
export class AptlyProjects { | ||
@@ -26,2 +27,8 @@ constructor(api, path, id) { | ||
} | ||
webhooks() { | ||
return new AptlyWebhooks(this.api, `${this.path}/${this.id}`); | ||
} | ||
webhook(webhookId) { | ||
return new AptlyWebhooks(this.api, `${this.path}/${this.id}`, webhookId); | ||
} | ||
get(query) { | ||
@@ -28,0 +35,0 @@ const search = new URLSearchParams(query); |
@@ -0,1 +1,2 @@ | ||
/// <reference types="node" /> | ||
import { AptlyApiQueries, AptlyDocument, AptlySearchPaginateResponse, AptlyUnit } from '@aptly-as/types'; | ||
@@ -19,2 +20,7 @@ import { AptlyApi } from './AptlyApi.js'; | ||
import(products: Partial<AptlyUnit>[]): Promise<AptlyUnit[]>; | ||
reportPDF(id?: string | undefined): Promise<{ | ||
buffer: Buffer; | ||
filename: string; | ||
contentType: string; | ||
}>; | ||
} |
@@ -34,2 +34,7 @@ import { AptlyDocuments } from './AptlyDocuments.js'; | ||
} | ||
async reportPDF(id = this.id) { | ||
if (!id) | ||
throw new Error('ID is required.'); | ||
return this.api.downloadDocument(`${this.path}/${id}/report.pdf`); | ||
} | ||
} |
{ | ||
"name": "@aptly-as/app-sdk", | ||
"version": "0.2.5", | ||
"version": "0.3.0", | ||
"description": "Aptly app SDK library for app communication and frontend development", | ||
@@ -5,0 +5,0 @@ "type": "module", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31683
27
793
14
5