Skip to content

Commit

Permalink
✨ Add JavaScript API
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed May 30, 2021
1 parent 68a190a commit 4dfe036
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 7 deletions.
2 changes: 1 addition & 1 deletion api/check_name_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { assertEquals } from "../dev_deps.ts";
import { RegisterableResult } from "../types/mod.ts";
import { REGISTRIES } from "../constants/registry.ts";
const BASE_URL = "https://registerable-pgkzeiwth-tomoki-miyauci.vercel.app/";
const BASE_URL = "https://registerable-cpqkjhmf3-tomoki-miyauci.vercel.app/";
const ENTPOINT = new URL(
"check-name",
BASE_URL,
Expand Down
10 changes: 5 additions & 5 deletions api/client.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const BASE_URL = "https://nameable-qmfotcl44-tomoki-miyauci.vercel.app/";
import type { RegisterableResult, Registry } from "../types/mod.ts";
import type { Option, RegisterableResult, Registry } from "../types/mod.ts";
import { stringify } from "./stringify.ts";

const client = async <T extends Registry>(
name: string,
option: Omit<Option, "mode">,
) => {
const url = new URL("check-name", BASE_URL);
url.searchParams.append("name", name);
const url = stringify({ name, ...option });

const res = await fetch(url.toString());
const res = await fetch(url);
return await res.json() as RegisterableResult<T>;
};

Expand Down
37 changes: 37 additions & 0 deletions api/client_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2021-present the Registerable authors. All rights reserved. MIT license.
import { assertEquals } from "../dev_deps.ts";
import { client } from "./client.ts";

Deno.test("client", async () => {
const table: [
...Parameters<typeof client>,
any,
][] = [
["fonction", { registry: ["deno.land"] }, {
error: {},
errorRegistry: [],
hasError: false,
name: "fonction",
result: {
"deno.land": false,
},
}],
["@@@", { registry: ["deno.land", "nest.land"] }, {
error: {
"deno.land": "Name contains only the characters a-z, 0-9 and _",
"nest.land": "Name contains only the characters a-z, 0-9 and _",
},
errorRegistry: ["deno.land", "nest.land"],
hasError: true,
name: "@@@",
result: {
"deno.land": false,
"nest.land": false,
},
}],
];

await Promise.all(table.map(async ([name, option, expected]) => {
assertEquals(await client(name, option), expected);
}));
});
24 changes: 24 additions & 0 deletions api/stringify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Registry } from "../types/mod.ts";
const BASE_URL = "https://registerable-cpqkjhmf3-tomoki-miyauci.vercel.app/";

const stringify = (
val: { name: string; registry: Registry[] },
): string => {
const url = new URL("check-name", BASE_URL);
url.search = constructSearchParams(val).toString();

return url.toString();
};

const constructSearchParams = (
{ name, registry }: { name: string; registry: Registry[] },
): URLSearchParams => {
const urlSearchParams = new URLSearchParams({ name });
registry.forEach((reg) => {
urlSearchParams.append("registry", reg);
});

return urlSearchParams;
};

export { BASE_URL, constructSearchParams, stringify };
39 changes: 39 additions & 0 deletions api/stringify_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { BASE_URL, constructSearchParams, stringify } from "./stringify.ts";
import { Registry } from "../types/mod.ts";

import { assertEquals } from "../dev_deps.ts";
Deno.test("stringify", () => {
const table: [{ name: string; registry: Registry[] }, string][] = [[{
name: "fonction",
registry: [],
}, `${BASE_URL}check-name?name=fonction`], [
{
name: "fonction",
registry: ["deno.land", "nest.land"],
},
`${BASE_URL}check-name?name=fonction&registry=deno.land&registry=nest.land`,
]];

table.forEach(([val, expected]) => {
assertEquals(stringify(val), expected);
});
});
Deno.test("constructSearchParams", () => {
const table: [{ name: string; registry: Registry[] }, string][] = [[
{ name: "hoge", registry: [] },
"name=hoge",
], [
{ name: "hoge", registry: ["npm"] },
"name=hoge&registry=npm",
], [
{ name: "hoge", registry: ["npm", "nest.land", "deno.land"] },
"name=hoge&registry=npm&registry=nest.land&registry=deno.land",
], [
{ name: "hoge", registry: ["nest.land", "npm"] },
"name=hoge&registry=nest.land&registry=npm",
]];

table.forEach(([val, expected]) => {
assertEquals(constructSearchParams(val).toString(), expected);
});
});
49 changes: 48 additions & 1 deletion check_name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,53 @@ const defaultOption: Option = {
registry: ["deno.land", "nest.land", "npm"],
};

/**
* Ask if the name can be registered in the package registry.
*
* @param name - Query name
* @param option - Query option
* @returns Result of registerable or not
*
* @remark
* Never throw an error
*
* @example
* ```ts
* // General usage
* await checkName('fonction')
* // {
* result: {
* "deno.land": false,
* "nest.land": false,
* npm: false,
* },
* hasError: false,
* error: {},
* errorRegistry: [],
* name: "fonction",
* };
* ```
*
* @example
* ```ts
* // Filter query registry
* await checkName('invalid-name', {
* registry: ['deno.land', 'npm']
* })
* // {
* result: {
* "deno.land": false,
* npm: false,
* },
* hasError: true,
* error: {
* "deno.land": "Name contains only the characters a-z, 0-9 and _"
* },
* errorRegistry: ["deno.land"],
* name: "invalid-name",
* };
* ```
*/
const checkName = async <T extends Registry>(
name: string,
option?: Partial<Option<T>>,
Expand All @@ -25,7 +72,7 @@ const checkName = async <T extends Registry>(
return await ifElse(
mode === "server",
async () => await query2Direct(query, name),
async () => await client(name),
async () => await client(name, { registry }),
);
};

Expand Down

0 comments on commit 4dfe036

Please sign in to comment.