@poap-xyz/providers
Advanced tools
Comparing version 0.0.9 to 0.0.10
/// <reference types="node" /> | ||
import { MediaStatus } from './constants'; | ||
import { CreateMomentResponse } from './../../ports/MomentsApiProvider/Types/response'; | ||
@@ -13,6 +14,7 @@ import { CreateMomentInput } from './../../ports/MomentsApiProvider/Types/input'; | ||
}>; | ||
uploadFile(file: Buffer, signedUrl: string): Promise<void>; | ||
waitForMediaProcessing(mediaKey: string): Promise<void>; | ||
uploadFile(file: Buffer, signedUrl: string, fileType: string): Promise<void>; | ||
fetchMediaStatus(mediaKey: string): Promise<MediaStatus>; | ||
waitForMediaProcessing(mediaKey: string, timeOut?: number): Promise<void>; | ||
createMoment(input: CreateMomentInput): Promise<CreateMomentResponse>; | ||
secureFetch(url: string, options: any): Promise<any>; | ||
} |
@@ -0,1 +1,2 @@ | ||
export { InvalidMediaFileError } from './core/PoapMomentsApi/errors/InvalidMediaFileError'; | ||
export { DropResponse } from './ports/DropApiProvider/Types/response'; | ||
@@ -2,0 +3,0 @@ export * from './ports'; |
@@ -9,4 +9,4 @@ /// <reference types="node" /> | ||
createMoment(input: CreateMomentInput): Promise<CreateMomentResponse>; | ||
uploadFile(file: Buffer, signedUrl: string): Promise<void>; | ||
waitForMediaProcessing(mediaKey: string): Promise<void>; | ||
uploadFile(file: Buffer, signedUrl: string, fileType: string): Promise<void>; | ||
waitForMediaProcessing(mediaKey: string, timeOut?: number): Promise<void>; | ||
} |
/// <reference types="node" /> | ||
import { MediaStatus } from './constants'; | ||
import { CreateMomentResponse } from './../../ports/MomentsApiProvider/Types/response'; | ||
@@ -13,6 +14,7 @@ import { CreateMomentInput } from './../../ports/MomentsApiProvider/Types/input'; | ||
}>; | ||
uploadFile(file: Buffer, signedUrl: string): Promise<void>; | ||
waitForMediaProcessing(mediaKey: string): Promise<void>; | ||
uploadFile(file: Buffer, signedUrl: string, fileType: string): Promise<void>; | ||
fetchMediaStatus(mediaKey: string): Promise<MediaStatus>; | ||
waitForMediaProcessing(mediaKey: string, timeOut?: number): Promise<void>; | ||
createMoment(input: CreateMomentInput): Promise<CreateMomentResponse>; | ||
secureFetch(url: string, options: any): Promise<any>; | ||
} |
@@ -0,1 +1,2 @@ | ||
export { InvalidMediaFileError } from './core/PoapMomentsApi/errors/InvalidMediaFileError'; | ||
export { DropResponse } from './ports/DropApiProvider/Types/response'; | ||
@@ -2,0 +3,0 @@ export * from './ports'; |
@@ -9,4 +9,4 @@ /// <reference types="node" /> | ||
createMoment(input: CreateMomentInput): Promise<CreateMomentResponse>; | ||
uploadFile(file: Buffer, signedUrl: string): Promise<void>; | ||
waitForMediaProcessing(mediaKey: string): Promise<void>; | ||
uploadFile(file: Buffer, signedUrl: string, fileType: string): Promise<void>; | ||
waitForMediaProcessing(mediaKey: string, timeOut?: number): Promise<void>; | ||
} |
/// <reference types="node" /> | ||
import { MediaStatus } from './constants'; | ||
import { CreateMomentResponse } from './../../ports/MomentsApiProvider/Types/response'; | ||
@@ -13,6 +14,7 @@ import { CreateMomentInput } from './../../ports/MomentsApiProvider/Types/input'; | ||
}>; | ||
uploadFile(file: Buffer, signedUrl: string): Promise<void>; | ||
waitForMediaProcessing(mediaKey: string): Promise<void>; | ||
uploadFile(file: Buffer, signedUrl: string, fileType: string): Promise<void>; | ||
fetchMediaStatus(mediaKey: string): Promise<MediaStatus>; | ||
waitForMediaProcessing(mediaKey: string, timeOut?: number): Promise<void>; | ||
createMoment(input: CreateMomentInput): Promise<CreateMomentResponse>; | ||
secureFetch(url: string, options: any): Promise<any>; | ||
} |
@@ -0,1 +1,2 @@ | ||
export { InvalidMediaFileError } from './core/PoapMomentsApi/errors/InvalidMediaFileError'; | ||
export { DropResponse } from './ports/DropApiProvider/Types/response'; | ||
@@ -2,0 +3,0 @@ export * from './ports'; |
@@ -7,2 +7,15 @@ (function (global, factory) { | ||
class InvalidMediaFileError extends Error { | ||
constructor() { | ||
super(`InvalidMediaFile error: This media file is invalid or cannot be processed.`); | ||
} | ||
} | ||
var MediaStatus; | ||
(function (MediaStatus) { | ||
MediaStatus["INVALID"] = "INVALID"; | ||
MediaStatus["PROCESSED"] = "PROCESSED"; | ||
MediaStatus["IN_PROCESS"] = "IN_PROCESS"; | ||
})(MediaStatus || (MediaStatus = {})); | ||
const MOMENTS_BASE_URL = 'https://moments.poap.tech'; | ||
@@ -20,3 +33,3 @@ class PoapMomentsApi { | ||
} | ||
async uploadFile(file, signedUrl) { | ||
async uploadFile(file, signedUrl, fileType) { | ||
return await this.secureFetch(signedUrl, { | ||
@@ -26,20 +39,36 @@ method: 'PUT', | ||
headers: { | ||
'Content-Type': 'application/octet-stream', | ||
'Content-Type': fileType, | ||
}, | ||
}); | ||
} | ||
async waitForMediaProcessing(mediaKey) { | ||
let status = 'IN_PROCESS'; | ||
await new Promise((resolve) => setTimeout(resolve, 2000)); | ||
while (status !== 'PROCESSED') { | ||
try { | ||
const response = await this.secureFetch(`${this.baseUrl}/media/${mediaKey}`, { | ||
method: 'GET', | ||
}); | ||
status = response.status; | ||
async fetchMediaStatus(mediaKey) { | ||
try { | ||
const response = await this.secureFetch(`${this.baseUrl}/media/${mediaKey}`, { | ||
method: 'GET', | ||
}); | ||
return response.status; | ||
} | ||
catch (_a) { | ||
return MediaStatus.IN_PROCESS; | ||
} | ||
} | ||
async waitForMediaProcessing(mediaKey, timeOut = 60000) { | ||
const delay = 2000; | ||
const maxTries = timeOut / delay; | ||
const checkStatus = async (tries) => { | ||
if (tries >= maxTries) { | ||
throw new Error('Exceeded maximum number of tries to check media processing status.'); | ||
} | ||
catch (error) { | ||
const status = await this.fetchMediaStatus(mediaKey); | ||
if (status === MediaStatus.PROCESSED) { | ||
return; | ||
} | ||
await new Promise((resolve) => setTimeout(resolve, 2000)); | ||
} | ||
if (status === MediaStatus.INVALID) { | ||
throw new InvalidMediaFileError(); | ||
} | ||
await new Promise((resolve) => setTimeout(resolve, delay)); | ||
return checkStatus(tries + 1); | ||
}; | ||
await new Promise((resolve) => setTimeout(resolve, delay)); | ||
return checkStatus(0); | ||
} | ||
@@ -190,2 +219,3 @@ async createMoment(input) { | ||
exports.InvalidMediaFileError = InvalidMediaFileError; | ||
exports.PoapCompass = PoapCompass; | ||
@@ -192,0 +222,0 @@ exports.PoapDropApi = PoapDropApi; |
@@ -9,4 +9,4 @@ /// <reference types="node" /> | ||
createMoment(input: CreateMomentInput): Promise<CreateMomentResponse>; | ||
uploadFile(file: Buffer, signedUrl: string): Promise<void>; | ||
waitForMediaProcessing(mediaKey: string): Promise<void>; | ||
uploadFile(file: Buffer, signedUrl: string, fileType: string): Promise<void>; | ||
waitForMediaProcessing(mediaKey: string, timeOut?: number): Promise<void>; | ||
} |
{ | ||
"name": "@poap-xyz/providers", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"description": "Providers module for the poap.js library", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/index.cjs", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
110901
86
1291