search_awesome/api/repo.ts

127 lines
3.4 KiB
TypeScript
Raw Normal View History

2022-12-01 19:40:03 +09:00
import { Client as ElasticsearchClient, SearchHit } from "https://deno.land/x/elasticsearch@v8.3.3/mod.ts";
import { config } from "https://deno.land/std@0.166.0/dotenv/mod.ts";
import { Doc } from "../doc_load/load.ts";
const env = await config({ export: true });
const client = new ElasticsearchClient({
node: "https://localhost:9200",
auth: {
username: env["ELASTIC_USERNAME"],
password: env["ELASTIC_PASSWORD"],
}
});
2022-11-29 01:52:44 +09:00
export interface RepoData {
name: string;
author: string;
description: string;
url: string;
stars: number;
forks: number;
tags: string[];
readme: string;
}
2022-12-01 19:40:03 +09:00
function docToRepoData(doc: Doc): RepoData {
return {
name: doc.name,
author: doc.author,
description: doc.desc,
url: doc.url,
stars: doc.star,
forks: doc.fork,
tags: doc.tags,
readme: doc.readme,
};
}
2022-11-29 01:52:44 +09:00
export const SAMPLE_DATA: RepoData[] = [
{
name: "deno",
author: "denoland",
description: "A secure runtime for JavaScript and TypeScript.",
url: "https://github.com/denoland/deno",
stars: 10000,
forks: 1000,
tags: ["deno", "runtime", "typescript"],
readme: `# Deno 🦕
Deno is a simple, modern and secure runtime for JavaScript and TypeScript that uses V8 and is built in Rust.
`,
},
{
name: "deno_std",
author: "denoland",
description: "Standard modules for the Deno runtime.",
url: "https://github.com/denoland/deno_std",
stars: 1000,
forks: 100,
tags: ["deno", "runtime", "typescript"],
readme: `# Deno Standard Modules 🦕
This repository contains the standard modules that are provided by default with Deno.
`,
},
{
name: "deno_website2",
author: "maximousblk",
description: "The new deno.land website.",
url: "https://github.com/maximousblk/deno_website2",
stars: 100,
forks: 10,
tags: ["deno", "website", "typescript"],
readme: `# Deno Website 2 🦕
The new deno.land website.
`,
}
]
export const getRepos = async (): Promise<RepoData[]> => {
2022-12-01 19:40:03 +09:00
const res = await client.search<Doc>({
target: "github-awesome",
body: {
query: {
match_all: {},
},
sort: [
{ "star": "desc" },
]
},
})
return res.hits.hits.map((hit: SearchHit<Doc>) => docToRepoData(hit._source));
2022-11-29 01:52:44 +09:00
}
export interface SearchOptions {
/**
* The limit of results to return.
* @default 10
* @minimum 1
* @maximum 100
*/
limit?: number;
/**
* The offset of results to return.
* @default 0
*/
offset?: number;
}
export async function searchRepos(query: string, options?: SearchOptions): Promise<RepoData[]> {
2022-12-01 19:40:03 +09:00
const res = await client.search<Doc>({
target: "github-awesome",
body: {
query: {
multi_match: {
query,
fields: ["name", "desc", "readme", "tags", "author"],
},
},
sort: [
"_score",
{ "star": "desc" },
],
from: options?.offset,
size: options?.limit,
},
});
//console.log(res);
return res.hits.hits.map((hit: SearchHit<Doc>) => docToRepoData(hit._source));
2022-11-29 01:52:44 +09:00
}