azure-computer-vision-wrapper
Advanced tools
Comparing version 1.0.1 to 1.32.0
{ | ||
"name": "azure-computer-vision-wrapper", | ||
"version": "1.0.1", | ||
"version": "1.32.0", | ||
"description": "A JavaScript / TypeScript wrappper library for the Azure Computer Vision API for demo purposes", | ||
@@ -8,3 +8,4 @@ "main": "src/index.js", | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"start": "npx tsc && node dist/index.js" | ||
"start": "npx tsc && node dist/index.js", | ||
"dev": "npx tsc && node dist/index.dev.js" | ||
}, | ||
@@ -11,0 +12,0 @@ "repository": { |
@@ -0,5 +1,16 @@ | ||
import Util from "./Util"; | ||
import AnalyzeImageParameters from "../interfaces/AnalyzeImage/AnalyzeImageParameters"; | ||
import AzureConfig from "../interfaces/AzureConfig"; | ||
import ReadParameters from "../interfaces/Read/ReadParameters"; | ||
import ReadResult from "../interfaces/Read/ReadResult"; | ||
import ClientNotInitializedError from "./errors/ClientNotInitializedError"; | ||
import AnalyzeImageResult from "../interfaces/AnalyzeImage/AnalyzeImageResult"; | ||
import DescribeImageParameters from "../interfaces/DescribeImage/DescribeImageParameters"; | ||
import DescribeImageResult from "../interfaces/DescribeImage/DescribeImageResult"; | ||
const CV_VERSION = "v3.2"; | ||
class ComputerVisionClient { | ||
private static azureConfig: AzureConfig; | ||
private _azureConfig?: AzureConfig; | ||
private _msEndpoint?: string; | ||
@@ -10,12 +21,125 @@ /** | ||
*/ | ||
constructor(azureConfig: AzureConfig) { | ||
ComputerVisionClient.azureConfig = azureConfig; | ||
constructor(azureConfig?: AzureConfig) { | ||
if (azureConfig) { | ||
this.azureConfig = azureConfig; | ||
} | ||
} | ||
public logKey(): void { | ||
console.log(this); | ||
console.log("apikey, ", ComputerVisionClient.azureConfig.apiKey); | ||
private get azureConfig(): AzureConfig { | ||
if (!this._azureConfig) { | ||
throw new ClientNotInitializedError(); | ||
} | ||
return this._azureConfig; | ||
} | ||
private set azureConfig(config: AzureConfig) { | ||
this._azureConfig = config; | ||
this._msEndpoint = `https://${this._azureConfig.endpoint}/vision/${CV_VERSION}`; | ||
} | ||
private get msEndpoint(): string { | ||
if (!this._msEndpoint) { | ||
throw new ClientNotInitializedError(); | ||
} | ||
return this._msEndpoint; | ||
} | ||
private getUrlWithParams<ParamsType extends {}>( | ||
endpoint: string, | ||
params?: ParamsType | ||
): URL { | ||
return Util.getUrlWithParams([this.msEndpoint, endpoint], params); | ||
} | ||
private async fetch( | ||
input: URL | RequestInfo, | ||
init?: RequestInit & { | ||
method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH"; | ||
} | ||
): Promise<Response> { | ||
init = init || {}; | ||
init.headers = { | ||
...init.headers, | ||
"Ocp-Apim-Subscription-Key": this.azureConfig.apiKey, | ||
"Content-type": "application/json", | ||
}; | ||
return await fetch(input, init); | ||
} | ||
private async fetchJSON<ReturnType>( | ||
input: URL | RequestInfo, | ||
init?: RequestInit & { | ||
method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH"; | ||
} | ||
): Promise<ReturnType> { | ||
return await (await this.fetch(input, init)).json(); | ||
} | ||
public async requestRead( | ||
imageUrl: string, | ||
params?: ReadParameters | ||
): Promise<{ operationLocation: string; res: Response }> { | ||
const endpoint = this.getUrlWithParams("/read/analyze", params); | ||
const res = await this.fetch(endpoint, { | ||
method: "POST", | ||
body: JSON.stringify({ url: imageUrl }), | ||
}); | ||
const operationLocation: string = | ||
res.headers.get("Operation-Location") || ""; | ||
return { operationLocation, res }; | ||
} | ||
public async getReadResult(url: string): Promise<ReadResult> { | ||
let res = await this.fetchJSON<ReadResult>(url); | ||
while (res.status === "notStarted" || res.status === "running") { | ||
await Util.wait(200); | ||
res = await this.fetchJSON(url); | ||
} | ||
return res; | ||
} | ||
public async read( | ||
imageUrl: string, | ||
params?: ReadParameters | ||
): Promise<ReadResult> { | ||
const operationLocation = (await this.requestRead(imageUrl, params)) | ||
.operationLocation; | ||
return await this.getReadResult(operationLocation); | ||
} | ||
public async analyzeImage( | ||
imageUrl: string, | ||
params?: AnalyzeImageParameters | ||
): Promise<AnalyzeImageResult> { | ||
const endpoint = this.getUrlWithParams("/analyze", params); | ||
return await this.fetchJSON<AnalyzeImageResult>(endpoint, { | ||
method: "POST", | ||
body: JSON.stringify({ url: imageUrl }), | ||
}); | ||
} | ||
public async describeImage( | ||
imageUrl: string, | ||
params?: DescribeImageParameters | ||
): Promise<DescribeImageResult> { | ||
const endpoint = this.getUrlWithParams("/describe", params); | ||
return await this.fetchJSON<DescribeImageResult>(endpoint, { | ||
method: "POST", | ||
body: JSON.stringify({ url: imageUrl }), | ||
}); | ||
} | ||
} | ||
export default ComputerVisionClient; |
import ComputerVisionClient from "./classes/ComputerVisionClient"; | ||
import type AzureConfig from "./interfaces/AzureConfig"; | ||
export default ComputerVisionClient; | ||
export let client = new ComputerVisionClient(); | ||
export function init(azureConfig: AzureConfig) { | ||
client = new ComputerVisionClient(azureConfig); | ||
return client; | ||
} |
{ | ||
"compilerOptions": { | ||
"target": "es6", | ||
"target": "ES2022", | ||
"module": "commonjs", | ||
@@ -5,0 +5,0 @@ "declaration": true, |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
16540
31
393
1
3
1