Compare commits
1
Commits
sidv/891
...
sidv/types
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c5d9cc8996 |
+1
-1
@@ -4,7 +4,7 @@ module.exports = {
|
||||
extends: [
|
||||
'eslint:recommended',
|
||||
'plugin:@typescript-eslint/recommended',
|
||||
// 'plugin:@typescript-eslint/recommended-requiring-type-checking',
|
||||
'plugin:@typescript-eslint/recommended-requiring-type-checking',
|
||||
'prettier'
|
||||
],
|
||||
plugins: ['svelte3', 'tailwindcss', '@typescript-eslint', 'es'],
|
||||
|
||||
@@ -5,14 +5,108 @@ import type { State } from '$lib/types';
|
||||
import { defaultState } from '../state';
|
||||
import { addHistoryEntry } from '../../components/history/history';
|
||||
|
||||
export interface GistResponse {
|
||||
url: string;
|
||||
forks_url: string;
|
||||
commits_url: string;
|
||||
id: string;
|
||||
node_id: string;
|
||||
git_pull_url: string;
|
||||
git_push_url: string;
|
||||
html_url: string;
|
||||
files: Files;
|
||||
public: boolean;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
description: string;
|
||||
comments: number;
|
||||
user: any;
|
||||
comments_url: string;
|
||||
owner: Owner;
|
||||
fork_of: any;
|
||||
forks: any[];
|
||||
history: History[];
|
||||
truncated: boolean;
|
||||
}
|
||||
|
||||
export interface Files {
|
||||
[key: string]: File;
|
||||
}
|
||||
|
||||
export interface File {
|
||||
filename: string;
|
||||
type: string;
|
||||
language: any;
|
||||
raw_url: string;
|
||||
size: number;
|
||||
truncated: boolean;
|
||||
content: string;
|
||||
}
|
||||
|
||||
export interface Owner {
|
||||
login: string;
|
||||
id: number;
|
||||
node_id: string;
|
||||
avatar_url: string;
|
||||
gravatar_id: string;
|
||||
url: string;
|
||||
html_url: string;
|
||||
followers_url: string;
|
||||
following_url: string;
|
||||
gists_url: string;
|
||||
starred_url: string;
|
||||
subscriptions_url: string;
|
||||
organizations_url: string;
|
||||
repos_url: string;
|
||||
events_url: string;
|
||||
received_events_url: string;
|
||||
type: string;
|
||||
site_admin: boolean;
|
||||
}
|
||||
|
||||
export interface History {
|
||||
user: User;
|
||||
version: string;
|
||||
committed_at: string;
|
||||
change_status: ChangeStatus;
|
||||
url: string;
|
||||
}
|
||||
|
||||
export interface User {
|
||||
login: string;
|
||||
id: number;
|
||||
node_id: string;
|
||||
avatar_url: string;
|
||||
gravatar_id: string;
|
||||
url: string;
|
||||
html_url: string;
|
||||
followers_url: string;
|
||||
following_url: string;
|
||||
gists_url: string;
|
||||
starred_url: string;
|
||||
subscriptions_url: string;
|
||||
organizations_url: string;
|
||||
repos_url: string;
|
||||
events_url: string;
|
||||
received_events_url: string;
|
||||
type: string;
|
||||
site_admin: boolean;
|
||||
}
|
||||
|
||||
export interface ChangeStatus {
|
||||
total: number;
|
||||
additions: number;
|
||||
deletions: number;
|
||||
}
|
||||
|
||||
const codeFileName = 'code.mmd';
|
||||
const configFileName = 'config.json';
|
||||
|
||||
const isValidGist = (files: any): boolean => {
|
||||
const isValidGist = (files: Files): boolean => {
|
||||
return codeFileName in files;
|
||||
};
|
||||
|
||||
const getFileContent = async (file: any): Promise<string> => {
|
||||
const getFileContent = async (file: File): Promise<string> => {
|
||||
if (file.truncated) {
|
||||
return await (await fetch(file.raw_url)).text();
|
||||
}
|
||||
@@ -32,9 +126,9 @@ const getGistData = async (gistURL: string): Promise<GistData> => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const [_, __, gistID, revisionID] = gistURL.split('github.com').pop().split('/');
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
const { html_url, files, history } = await (
|
||||
const { html_url, files, history } = (await (
|
||||
await fetch(`https://api.github.com/gists/${gistID}${revisionID ? '/' + revisionID : ''}`)
|
||||
).json();
|
||||
).json()) as GistResponse;
|
||||
if (isValidGist(files)) {
|
||||
const code = await getFileContent(files[codeFileName]);
|
||||
let config: string;
|
||||
@@ -48,7 +142,7 @@ const getGistData = async (gistURL: string): Promise<GistData> => {
|
||||
config,
|
||||
author: currentItem.user.login,
|
||||
time: new Date(currentItem.committed_at).getTime(),
|
||||
version: (currentItem.version as string).slice(-7)
|
||||
version: currentItem.version.slice(-7)
|
||||
};
|
||||
} else {
|
||||
throw 'Invalid gist provided';
|
||||
|
||||
Reference in New Issue
Block a user