Skip to content
This repository has been archived by the owner on Jan 16, 2022. It is now read-only.

Commit

Permalink
fix: added packageMeta type
Browse files Browse the repository at this point in the history
  • Loading branch information
Griffith Tchen Pan authored and griffithtp committed Jun 24, 2019
1 parent c0b0189 commit 3c54b11
Show file tree
Hide file tree
Showing 16 changed files with 83 additions and 65 deletions.
12 changes: 6 additions & 6 deletions src/components/Author/Author.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Component, ReactNode } from 'react';
import React, { Component, ReactNode, ReactElement } from 'react';

import Avatar from '@material-ui/core/Avatar';
import List from '@material-ui/core/List';
Expand All @@ -8,18 +8,18 @@ import { DetailContextConsumer } from '../../pages/version/Version';
import { Heading, AuthorListItem } from './styles';
import { isEmail } from '../../utils/url';

class Authors extends Component<any, any> {
render() {
class Authors extends Component {
public render(): ReactElement<HTMLElement> {
return (
<DetailContextConsumer>
{(context: any) => {
{context => {
return context && context.packageMeta && this.renderAuthor(context.packageMeta);
}}
</DetailContextConsumer>
);
}

renderLinkForMail(email: string, avatarComponent: ReactNode, packageName: string, version: string) {
public renderLinkForMail(email: string, avatarComponent: ReactNode, packageName: string, version: string): ReactElement<HTMLElement> | ReactNode {
if (!email || isEmail(email) === false) {
return avatarComponent;
}
Expand All @@ -31,7 +31,7 @@ class Authors extends Component<any, any> {
);
}

renderAuthor = packageMeta => {
public renderAuthor = packageMeta => {
const { author, name: packageName, version } = packageMeta.latest;

if (!author) {
Expand Down
7 changes: 4 additions & 3 deletions src/components/Package/Package.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ interface Dist {
unpackedSize: number;
}

interface Props {
export interface PackageInterface {
name: string;
version: string;
time: string;
time?: number | string;
author: Author;
description?: string;
keywords?: string[];
Expand All @@ -35,6 +35,7 @@ interface Props {
bugs?: Bugs;
dist?: Dist;
}
// interface Props {} & PackageInterface;

import {
Author,
Expand All @@ -56,7 +57,7 @@ import {
} from './styles';
import { isURL } from '../../utils/url';

const Package: React.FC<Props> = ({
const Package: React.FC<PackageInterface> = ({
author: { name: authorName, avatar: authorAvatar },
bugs,
description,
Expand Down
21 changes: 8 additions & 13 deletions src/components/PackageList/PackageList.tsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,41 @@
import React, { Fragment } from 'react';
import PropTypes from 'prop-types';
import React, { Fragment, ReactElement } from 'react';

import Divider from '@material-ui/core/Divider';

import Package from '../Package';
import Help from '../Help';
import { formatLicense } from '../../utils/package';
import { PackageInterface } from '../Package/Package';

// @ts-ignore
import classes from './packageList.scss';

interface Props {
packages: any;
packages: PackageInterface[];
}

export default class PackageList extends React.Component<Props, {}> {
static propTypes = {
packages: PropTypes.array,
};

render() {
public render(): ReactElement<HTMLElement> {
return (
<div className={'package-list-items'}>
<div className={classes.pkgContainer}>{this.hasPackages() ? this.renderPackages() : <Help />}</div>
</div>
);
}

hasPackages() {
private hasPackages(): boolean {
const { packages } = this.props;
return packages.length > 0;
}

renderPackages = () => {
private renderPackages = () => {
return <>{this.renderList()}</>;
};

renderList = () => {
private renderList = () => {
const { packages } = this.props;
return packages.map((pkg, i) => {
const { name, version, description, time, keywords, dist, homepage, bugs } = pkg;
const author = pkg.author;
const { name, version, description, time, keywords, dist, homepage, bugs, author } = pkg;
// TODO: move format license to API side.
const license = formatLicense(pkg.license);
return (
Expand Down
9 changes: 2 additions & 7 deletions src/components/Repository/Repository.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Heading, GithubLink, RepositoryListItem } from './styles';
import git from './img/git.png';
import { isURL } from '../../utils/url';

class Repository extends Component<any, any> {
class Repository extends Component {
public render(): ReactElement<HTMLElement> {
return (
<DetailContextConsumer>
Expand All @@ -33,12 +33,7 @@ class Repository extends Component<any, any> {
}

private renderRepository = packageMeta => {
const {
repository: {
// @ts-ignore
url,
} = {},
} = packageMeta.latest;
const { repository: { url = null } = {} } = packageMeta.latest;

if (!url || isURL(url) === false) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/components/UpLinks/UpLinks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { formatDateDistance } from '../../utils/package';

import { Heading, Spacer, ListItemText } from './styles';

class UpLinks extends React.PureComponent<any> {
class UpLinks extends React.PureComponent<{}> {
public render(): ReactElement<HTMLElement> {
return (
<DetailContextConsumer>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import PackageList from '../../components/PackageList';

interface Props {
isUserLoggedIn: boolean;
packages: any[];
packages: [];
}

const Home: React.FC<Props> = ({ packages }) => (
Expand Down
41 changes: 27 additions & 14 deletions src/pages/version/Version.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,44 @@ import Grid from '@material-ui/core/Grid';
import Loading from '../../components/Loading/Loading';
import DetailContainer from '../../components/DetailContainer/DetailContainer';
import DetailSidebar from '../../components/DetailSidebar/DetailSidebar';
import { callDetailPage, DetailPage } from '../../utils/calls';
import { callDetailPage } from '../../utils/calls';
import { getRouterPackageName } from '../../utils/package';
import NotFound from '../../components/NotFound';
import { PackageMetaInterface } from '../../../types/packageMeta';

export interface DetailContextProps {
packageMeta: any;
readMe: any;
packageMeta: PackageMetaInterface;
readMe: string;
packageName: string;
enableLoading: () => void;
}

export const DetailContext = React.createContext<DetailContextProps | null>(null);
export const DetailContext = React.createContext<Partial<DetailContextProps>>({});

export interface VersionPageConsumerProps {
packageMeta: any;
readMe: any;
packageName: any;
enableLoading: any;
packageMeta: PackageMetaInterface;
readMe: string;
packageName: string;
enableLoading: () => void;
}

export const DetailContextProvider: Provider<VersionPageConsumerProps | null> = DetailContext.Provider;
export const DetailContextConsumer: Consumer<VersionPageConsumerProps | null> = DetailContext.Consumer;
export const DetailContextProvider: Provider<Partial<VersionPageConsumerProps>> = DetailContext.Provider;
export const DetailContextConsumer: Consumer<Partial<VersionPageConsumerProps>> = DetailContext.Consumer;

interface PropsInterface {
match: boolean;
}

interface StateInterface {
readMe: string;
packageName: string;
packageMeta: PackageMetaInterface | null;
isLoading: boolean;
notFound: boolean;
}

class VersionPage extends Component<any, any> {
constructor(props: any) {
class VersionPage extends Component<PropsInterface, StateInterface> {
constructor(props) {
super(props);

this.state = {
Expand All @@ -39,7 +52,7 @@ class VersionPage extends Component<any, any> {
};
}

public static getDerivedStateFromProps(nextProps: any, prevState: any): any {
public static getDerivedStateFromProps(nextProps, prevState): { packageName?: string; isLoading: boolean; notFound?: boolean } | null {
const { match } = nextProps;
const packageName = getRouterPackageName(match);

Expand All @@ -65,7 +78,7 @@ class VersionPage extends Component<any, any> {
}

/* eslint no-unused-vars: 0 */
public async componentDidUpdate(nextProps: any, prevState: any): Promise<void> {
public async componentDidUpdate(nextProps, prevState: StateInterface): Promise<void> {
const { packageName } = this.state;
if (packageName !== prevState.packageName) {
const { readMe, packageMeta } = await callDetailPage(packageName);
Expand Down
7 changes: 6 additions & 1 deletion src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ const NotFound = asyncComponent(() => import('./components/NotFound'));
const VersionPackage = asyncComponent(() => import('./pages/version/Version'));
const HomePage = asyncComponent(() => import('./pages/home'));

class RouterApp extends Component<any, any> {
interface RouterAppProps {
onLogout: () => void;
onToggleLoginModal: () => void;
}

class RouterApp extends Component<RouterAppProps> {
public render(): ReactElement<HTMLDivElement> {
return (
<Router history={history}>
Expand Down
4 changes: 2 additions & 2 deletions src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import '../../types';
* @param {object} response
* @returns {promise}
*/
function handleResponseType(response): Promise<any> {
function handleResponseType(response: Response): Promise<[boolean, Blob | string]> | Promise<void> {
if (response.headers) {
const contentType = response.headers.get('Content-Type');
const contentType = response.headers.get('Content-Type') as string;
if (contentType.includes('application/pdf')) {
return Promise.all([response.ok, response.blob()]);
}
Expand Down
5 changes: 3 additions & 2 deletions src/utils/calls.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import API from './api';
import { PackageMetaInterface } from 'types/packageMeta';

export interface DetailPage {
readMe: any;
packageMeta: any;
readMe: string;
packageMeta: PackageMetaInterface;
}

export async function callDetailPage(packageName): Promise<DetailPage> {
Expand Down
1 change: 1 addition & 0 deletions src/utils/file-size.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* tslint:disable */
export default function fileSizeSI(a?: any, b?: any, c?: any, d?: any, e?: any) {
return ((b = Math), (c = b.log), (d = 1e3), (e = (c(a) / c(d)) | 0), a / b.pow(d, e)).toFixed(2) + ' ' + (e ? 'kMGTPEZY'[--e] + 'B' : 'Bytes');
}
18 changes: 11 additions & 7 deletions src/utils/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,34 @@ import { Base64 } from 'js-base64';
import API from './api';
import { HEADERS } from '../../lib/constants';

export function isTokenExpire(token?: any) {
interface PayloadInterface {
exp: number;
}

export function isTokenExpire(token?: string): boolean {
if (!isString(token)) {
return true;
}

let [, payload]: any = token.split('.');
const [, payload] = token.split('.');

if (!payload) {
return true;
}

let exp: number;
try {
payload = JSON.parse(Base64.decode(payload));
exp = JSON.parse(Base64.decode(payload)).exp;
} catch (error) {
// eslint-disable-next-line
console.error('Invalid token:', error, token);
console.error('Invalid token:', error, token);
return true;
}

if (!payload.exp || !isNumber(payload.exp)) {
if (!exp || !isNumber(exp)) {
return true;
}
// Report as expire before (real expire time - 30s)
const jsTimestamp = payload.exp * 1000 - 30000;
const jsTimestamp = exp * 1000 - 30000;
const expired = Date.now() >= jsTimestamp;

return expired;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/sec-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import parseXSS from 'xss';

export function preventXSS(text: string) {
export function preventXSS(text: string): string {
const encodedText = parseXSS.filterXSS(text);

return encodedText;
Expand Down
4 changes: 2 additions & 2 deletions src/utils/styles/mixings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* CSS to represent truncated text with an ellipsis.
*/
export function ellipsis(width: string | number) {
export function ellipsis(width: string | number): {} {
return {
display: 'inline-block',
maxWidth: width,
Expand All @@ -24,7 +24,7 @@ interface SpacingShortHand<type> {

const positionMap = ['Top', 'Right', 'Bottom', 'Left'];

export function spacing(property: 'padding' | 'margin', ...values: SpacingShortHand<number | string>[]) {
export function spacing(property: 'padding' | 'margin', ...values: SpacingShortHand<number | string>[]): {} {
const [firstValue = 0, secondValue = 0, thirdValue = 0, fourthValue = 0] = values;
const valuesWithDefaults = [firstValue, secondValue, thirdValue, fourthValue];
let styles = {};
Expand Down
7 changes: 2 additions & 5 deletions types/custom.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// https://stackoverflow.com/questions/44717164/unable-to-import-svg-files-in-typescript
declare module '*.svg' {
const content: any;
const content: string;
export default content;
}

declare module '*.png' {
const content: any;
export default content;
}
declare module '*.png';
6 changes: 6 additions & 0 deletions types/packageMeta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface PackageMetaInterface {
latest: {
name: string;
};
_uplinks: {};
}

0 comments on commit 3c54b11

Please sign in to comment.