Files
vercel/api/examples/info.ts
Steven e5cc1d643a [frameworks][api] Add sort order (#4829)
This PR adds an optional property called `sort` to each framework so that we can change the order returned in the API. 
The reason this is necessary is because the order of the original array determines the precedence of framework detection. So we need another way to indicate the order of templates/examples returned from the API.

In particular, we need "Next.js" to be first and "Other" to be last.

I also updated the deprecated `@now/node` usage to `@vercel/node` in the API.
2020-07-10 18:09:14 +00:00

45 lines
1.2 KiB
TypeScript

// A proxy to get the basic info of an existing github/gitlab repo:
// GET /info?repo=vercel/micro
// @ts-ignore
import parseGitUrl from 'parse-github-url';
import { NowRequest, NowResponse } from '@vercel/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)) || {});
});