Files
vercel/api/examples/info.ts
Andy 890de6a625 Add API for frameworks and examples (#3514)
* Add API for frameworks and examples

* Adjust headers

* Update frameworks list

* Always use latest

* Add types

* Use now repo for downloading and listing

* Use .existsSync

* Remove unused packages

* Use 307 for redirect

* Add examples

* Update tsconfig.json

Co-Authored-By: Steven <steven@ceriously.com>

* Make examples unique

* Remove detectors from frameworks API

* Use /api instead of Next.js

* Install dependencies

* Rename project

* Change name

* Empty

* Change name

* Update api/tsconfig.json

Co-Authored-By: Steven <steven@ceriously.com>

* Update examples

Co-authored-by: Steven <steven@ceriously.com>
2020-01-07 23:55:39 +01:00

45 lines
1.2 KiB
TypeScript

// A proxy to get the basic info of an existing github/gitlab repo:
// GET /info?repo=zeit/micro
// @ts-ignore
import parseGitUrl from 'parse-github-url';
import { NowRequest, NowResponse } from '@now/node';
import { withApiHandler } from '../_lib/util/with-api-handler';
import { getGitHubRepoInfo } from '../_lib/examples/github-repo-info';
import { getGitLabRepoInfo } from '../_lib/examples/gitlab-repo-info';
export default withApiHandler(async function(
req: NowRequest,
res: NowResponse
) {
const repoPath = decodeURIComponent((req.query.repo as string) || '');
if (!repoPath) {
return res.status(404).json({
error: {
code: 'not_found',
message: 'Please provide the `repo` parameter.',
},
});
}
const repo = parseGitUrl(repoPath);
if (!repo.repo) {
return res.status(400).json({
error: {
code: 'invalid_repo_url',
message: 'Repository URL is invalid.',
},
});
}
if (repo.host === 'github.com') {
// URL is 'https://github.com/user/repo' or 'user/repo'
return res.json((await getGitHubRepoInfo(repo)) || {});
}
// gitlab
res.json((await getGitLabRepoInfo(repo)) || {});
});