UNPKG

@kduprey/perspective-api-client

Version:
1 lines 6.54 kB
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import axios, { AxiosError } from \"axios\";\nimport _ from \"lodash\";\nimport type {\n\tAnalyzeCommentRequest,\n\tAnalyzeResponse,\n\tAttributeType,\n\tCommentRequestOptions,\n\tRequestedAttribute,\n} from \"./types\";\nimport ISO6391 from \"iso-639-1\";\n\nconst COMMENT_ANALYZER_URL =\n\t\"https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze\";\nexport const MAX_LENGTH = 20480;\n\nclass PerspectiveAPIClientError extends Error {\n\tconstructor(message: string) {\n\t\tsuper(message);\n\t\tError.captureStackTrace(this, this.constructor);\n\t\tthis.name = \"PerspectiveAPIClientError\";\n\t}\n}\n\nclass TextEmptyError extends PerspectiveAPIClientError {\n\tconstructor() {\n\t\tsuper(\"text must not be empty\");\n\t\tthis.name = \"TextEmptyError\";\n\t}\n}\n\nclass TextTooLongError extends PerspectiveAPIClientError {\n\tconstructor() {\n\t\tsuper(\n\t\t\t`text must not be greater than ${MAX_LENGTH.toString()} characters in length`\n\t\t);\n\t\tthis.name = \"TextTooLongError\";\n\t}\n}\n\nclass ResponseError extends PerspectiveAPIClientError {\n\tresponse: AxiosError;\n\n\tconstructor(message: string, response: AxiosError) {\n\t\tsuper(message);\n\t\tthis.response = response;\n\t\tthis.name = \"ResponseError\";\n\t}\n}\n\nclass Perspective {\n\tstatic readonly PerspectiveAPIClientError: typeof PerspectiveAPIClientError;\n\tstatic readonly TextEmptyError: typeof TextEmptyError;\n\tstatic readonly TextTooLongError: typeof TextTooLongError;\n\tstatic readonly ResponseError: typeof ResponseError;\n\n\tapiKey: string;\n\n\tconstructor({ apiKey }: { apiKey: string }) {\n\t\tthis.apiKey = apiKey;\n\t\tif (!this.apiKey) {\n\t\t\tthrow new Error(\"Must provide options.apiKey\");\n\t\t}\n\t}\n\n\tasync analyze(\n\t\ttext: string,\n\t\tattriubtes?:\n\t\t\t| Partial<Record<AttributeType, RequestedAttribute>>\n\t\t\t| AttributeType[],\n\t\toptions?: CommentRequestOptions\n\t): Promise<AnalyzeResponse> {\n\t\tconst request: AnalyzeCommentRequest = {\n\t\t\tcomment: { text },\n\t\t\trequestedAttributes: this.parseAttributes(attriubtes ?? { TOXICITY: {} }),\n\t\t\t...options,\n\t\t};\n\n\t\tthis.validateComment(text);\n\t\tif (options?.languages) {\n\t\t\tthis.validateLanguages(options.languages);\n\t\t\trequest.languages = options.languages;\n\t\t}\n\n\t\tconst response = await axios\n\t\t\t.post<AnalyzeResponse>(COMMENT_ANALYZER_URL, request, {\n\t\t\t\tparams: { key: this.apiKey },\n\t\t\t})\n\t\t\t.catch((error: unknown) => {\n\t\t\t\tif (axios.isAxiosError(error)) {\n\t\t\t\t\tconst responseError = new ResponseError(error.message, error);\n\t\t\t\t\tthrow responseError;\n\t\t\t\t}\n\t\t\t\tconsole.error(\"Unknown error\", error);\n\t\t\t\treturn Promise.reject(new Error(\"Unknown error - see logs\"));\n\t\t\t});\n\t\treturn response.data;\n\t}\n\n\tvalidateComment(text: string): void {\n\t\tif (!text) {\n\t\t\tthrow new TextEmptyError();\n\t\t}\n\t\tif (text.length > MAX_LENGTH) {\n\t\t\tthrow new TextTooLongError();\n\t\t}\n\t}\n\n\tvalidateLanguages(languages: string[]): void {\n\t\t// Make sure requested languages are valid, if provided\n\t\tlanguages.forEach((language) => {\n\t\t\tif (ISO6391.validate(language)) {\n\t\t\t\tthrow new PerspectiveAPIClientError(\n\t\t\t\t\t`language ${language} is not supported`\n\t\t\t\t);\n\t\t\t}\n\t\t});\n\t}\n\n\tparseAttributes(\n\t\tattributes:\n\t\t\t| Partial<Record<AttributeType, RequestedAttribute>>\n\t\t\t| Partial<AttributeType>[]\n\t): Partial<Record<AttributeType, RequestedAttribute>> {\n\t\t// Convert attributes to array of objects\n\t\tif (_.isArray(attributes)) {\n\t\t\treturn _.reduce<\n\t\t\t\tAttributeType,\n\t\t\t\tPartial<Record<AttributeType, RequestedAttribute>>\n\t\t\t>(\n\t\t\t\tattributes,\n\t\t\t\t(acc, attribute) => {\n\t\t\t\t\tacc[attribute] = {};\n\t\t\t\t\treturn acc;\n\t\t\t\t},\n\t\t\t\t{}\n\t\t\t);\n\t\t}\n\t\t// Make sure requested attributes score type is between 0 and 1\n\t\tObject.values(attributes).forEach((attribute) => {\n\t\t\tif (\n\t\t\t\tattribute.scoreThreshold &&\n\t\t\t\t(attribute.scoreThreshold < 0 || attribute.scoreThreshold > 1)\n\t\t\t) {\n\t\t\t\tthrow new PerspectiveAPIClientError(\n\t\t\t\t\t\"scoreThreshold must be between 0 and 1\"\n\t\t\t\t);\n\t\t\t}\n\t\t});\n\t\treturn attributes;\n\t}\n}\nexport {\n\tResponseError,\n\tTextEmptyError,\n\tTextTooLongError,\n\tPerspectiveAPIClientError,\n};\nexport default Perspective;\n"],"mappings":";AAAA,OAAO,WAA2B;AAClC,OAAO,OAAO;AAQd,OAAO,aAAa;AAEpB,IAAM,uBACL;AACM,IAAM,aAAa;AAE1B,IAAM,4BAAN,cAAwC,MAAM;AAAA,EAC7C,YAAY,SAAiB;AAC5B,UAAM,OAAO;AACb,UAAM,kBAAkB,MAAM,KAAK,WAAW;AAC9C,SAAK,OAAO;AAAA,EACb;AACD;AAEA,IAAM,iBAAN,cAA6B,0BAA0B;AAAA,EACtD,cAAc;AACb,UAAM,wBAAwB;AAC9B,SAAK,OAAO;AAAA,EACb;AACD;AAEA,IAAM,mBAAN,cAA+B,0BAA0B;AAAA,EACxD,cAAc;AACb;AAAA,MACC,iCAAiC,WAAW,SAAS,CAAC;AAAA,IACvD;AACA,SAAK,OAAO;AAAA,EACb;AACD;AAEA,IAAM,gBAAN,cAA4B,0BAA0B;AAAA,EACrD;AAAA,EAEA,YAAY,SAAiB,UAAsB;AAClD,UAAM,OAAO;AACb,SAAK,WAAW;AAChB,SAAK,OAAO;AAAA,EACb;AACD;AAEA,IAAM,cAAN,MAAkB;AAAA,EACjB,OAAgB;AAAA,EAChB,OAA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