Skip to content

Commit

Permalink
Properly Document Sagiri this time in JSDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
sr229 authored Aug 26, 2024
1 parent adadaea commit 7600be8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
4 changes: 2 additions & 2 deletions jsr.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@clarity/sagiri",
"version": "4.0.1",
"exports": "./lib/mod.ts"
"version": "4.0.2",
"exports": "./lib/sagiri.ts"
}
18 changes: 13 additions & 5 deletions lib/sagiri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createReadStream } from 'node:fs';
import FormData from 'form-data';
import { generateMask, resolveResult } from './util';
import { SagiriClientError, SagiriServerError } from './errors';
import type { IResponse } from './response';
import type { IResponse, IResult } from './response';
import sites from './sites';

let fetchFn;
Expand All @@ -19,14 +19,23 @@ if (globalThis.fetch === undefined) {

type File = string | Buffer | Readable;

const sagiri = (token: string, defaultOpts: IOptions = { results: 5 }) => {
/**
* Creates a function to be used for finding potential sources for a given image.
* By default has options set to give 5 results from SauceNAO.
* @param token your saucenao token, get one from https://saucenao.com/user.php
* @param defaultOpts the default options that the client will use for querying
* @returns an `async function (file: File, optionOverrides?: Options)` which is loaded with the given token and default options to use.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const sagiri = (token: string, defaultOpts: IOptions = { results: 5 }): (file: File, opts?: IOptions) => Promise<{ url: any; site: any; index: number; similarity: number; thumbnail: string; authorName: any; authorUrl: any; raw: IResult; }[]> => {
console.debug(`Created sagiri instance with opts: ${JSON.stringify(defaultOpts)}`);

// token validation to ensure the token is 40 characters long and alphanumeric
if (env.NODE_ENV !== "test")
if (token.length < 40 || !/^[a-zA-Z0-9]+$/.test(token))
throw new Error("Malformed token. Get a token from https://saucenao.com/user.php");


return async (file: File, opts: IOptions = {}) => {
if (!file) throw new Error("No file provided");

Expand Down Expand Up @@ -81,15 +90,14 @@ const sagiri = (token: string, defaultOpts: IOptions = { results: 5 }) => {
headers: form.getHeaders()
});

// I'm sure there's a better way to do this but I'll just re-assign a new var
// because I am writing this in midnight and I want to go to sleep
const res = await response.json() as IResponse;

const {
header: { status, message, results_returned: resultsReturned },
} = res;

// I'm sure there's a better way to do this but I'll just re-assign a new var
// because I am writing this in midnight and I want to go to sleep


// server-side error
if (status > 0) throw new SagiriServerError(status, message!);
Expand Down

0 comments on commit 7600be8

Please sign in to comment.