Skip to content

Commit

Permalink
refactor: clean up dirty naming
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshinorin committed Jan 31, 2024
1 parent 291394e commit 57148d8
Show file tree
Hide file tree
Showing 18 changed files with 51 additions and 51 deletions.
19 changes: 0 additions & 19 deletions __tests__/unit/utils/filterRequests.test.ts

This file was deleted.

19 changes: 19 additions & 0 deletions __tests__/unit/utils/match.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { expect, test } from 'vitest'
import { isMatch } from '../../../src/utils/match';

test('isMatch should be return true', () => {
expect(isMatch('favicon.svg')).toBeTruthy();
expect(isMatch('a.svg')).toBeTruthy();
expect(isMatch('b.svg/')).toBeTruthy();
expect(isMatch('c.jpg')).toBeTruthy();
expect(isMatch('d.jpg/')).toBeTruthy();
});

test('isMatch should be return false', () => {
expect(isMatch('/')).toBeFalsy();
expect(isMatch('/articles/')).toBeFalsy();
expect(isMatch('.jpg')).toBeFalsy();
expect(isMatch('/yyyy/mm/dd/')).toBeFalsy();
expect(isMatch('jpg')).toBeFalsy();
expect(isMatch('svg')).toBeFalsy();
});
4 changes: 2 additions & 2 deletions src/api/request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RequestContext } from '../models/models';
import { isIgnoreRequest } from '../utils/filterRequests';
import { isMatch } from '../utils/match';

// TODO: write test code
// TODO: more simply
Expand All @@ -15,7 +15,7 @@ export async function fetchFromApi(
// TODO: extract to function & write test code
if (options) {
if (options.interceptIfContainsIgnorePaths) {
if (!endpoint || (endpoint && isIgnoreRequest(endpoint))) {
if (!endpoint || (endpoint && isMatch(endpoint))) {
return new Response(null, { "status" : 404 });
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/[...slug]/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const Renderer: React.FunctionComponent<{
return (
<>
<CoverComponent
contentCover={{
props={{
title: content.title,
tags: null,
publishedAt: content.publishedAt,
Expand Down
2 changes: 1 addition & 1 deletion src/app/archives/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const Renderer: React.FunctionComponent<{
return (
<>
<CoverComponent
contentCover={{
props={{
title: "Archives",
tags: null,
publishedAt: null,
Expand Down
4 changes: 2 additions & 2 deletions src/app/articles/[...slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { headers } from 'next/headers';
import { Metadata } from 'next'
import { notFound } from "next/navigation";
import { ContentResponse, Content, ContentResponseWithFetchResponse } from '../../../models/models';
import { isIgnoreRequest } from '../../../utils/filterRequests';
import { isMatch } from '../../../utils/match';
import { fetchFromApi } from '../../../api/request';
import { asInsight } from '../../../utils/insight';
import { Renderer } from './renderer';
Expand Down Expand Up @@ -51,7 +51,7 @@ async function run(req: any): Promise<any> {

async function handler(req: any) {
// NOTE: avoid send request of images
if (isIgnoreRequest(req.params.slug.join("/"))) {
if (isMatch(req.params.slug.join("/"))) {
return notFound();
}
const sluggized = await sluggize([PREFIX_URL].concat(req.params.slug));
Expand Down
2 changes: 1 addition & 1 deletion src/app/articles/[...slug]/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const Renderer: React.FunctionComponent<{
return (
<>
<CoverComponent
contentCover={{
props={{
title: content.title,
tags: content.tags,
publishedAt: content.publishedAt,
Expand Down
2 changes: 1 addition & 1 deletion src/app/articles/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const Renderer: React.FunctionComponent<{
return (
<>
<CoverComponent
contentCover={null}
props={null}
/>
<main>
<ArticlesComponent
Expand Down
2 changes: 1 addition & 1 deletion src/app/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const Renderer: React.FunctionComponent<{
return (
<>
<CoverComponent
contentCover={null}
props={null}
/>
<main>
<div className={`${containerStyles.container} ${styles.wrap}`} >
Expand Down
2 changes: 1 addition & 1 deletion src/app/search/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const Renderer: React.FunctionComponent<{
return (
<>
<CoverComponent
contentCover={{
props={{
title: "Search (β)",
tags: null,
publishedAt: null,
Expand Down
2 changes: 1 addition & 1 deletion src/app/series/[slug]/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const Renderer: React.FunctionComponent<{
return (
<>
<CoverComponent
contentCover={{
props={{
title: seriresWithArticles.title,
tags: null,
publishedAt: null,
Expand Down
2 changes: 1 addition & 1 deletion src/app/series/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const Renderer: React.FunctionComponent<{
return (
<>
<CoverComponent
contentCover={{
props={{
title: "Series",
tags: null,
publishedAt: null,
Expand Down
2 changes: 1 addition & 1 deletion src/app/settings/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function Page(){
return (
<>
<CoverComponent
contentCover={{
props={{
title: "Settings",
tags: null,
publishedAt: null,
Expand Down
2 changes: 1 addition & 1 deletion src/app/tags/[...slug]/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const Renderer: React.FunctionComponent<{
return (
<>
<CoverComponent
contentCover={{
props={{
title: tagName,
tags: null,
publishedAt: null,
Expand Down
2 changes: 1 addition & 1 deletion src/app/tags/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const Renderer: React.FunctionComponent<{
return (
<>
<CoverComponent
contentCover={{
props={{
title: "Tags",
tags: null,
publishedAt: null,
Expand Down
22 changes: 14 additions & 8 deletions src/components/cover.tsx
Original file line number Diff line number Diff line change
@@ -1,41 +1,47 @@
import Link from 'next/link';
import { TagComponent } from './tag';
import { NavigationComponent } from './navigation';
import { ContentCover, Tag } from '../models/models';
import { Tag } from '../models/models';
import styles from '../styles/components/cover.module.scss';
import { toDate } from '../utils/time';
import { title, subTitle, coverBottomItems } from '../../config';

export const CoverComponent: React.FunctionComponent<{ contentCover: ContentCover | null }> = ({ contentCover }) => {
type Props = {
title: string,
tags?: Array<Tag>,
publishedAt: number,
}

export const CoverComponent: React.FunctionComponent<{ props: Props | null }> = ({ props }) => {
return (
<>
<div className={styles['cover']}>
<div className={`${styles['content-header']}`}>
{
(() => {
if (contentCover) {
if (props) {
return(
<>
<h1 className={`${styles['content-title']}`}>
{contentCover.title}
{props.title}
</h1>
{
(() => {
if (contentCover.publishedAt) {
if (props.publishedAt) {
return(
<span className={`${styles['content-meta']}`}>
{ toDate(contentCover.publishedAt).toUTCString() }
{ toDate(props.publishedAt).toUTCString() }
</span>
)
}
})()
}
{
(() => {
if (contentCover.tags) {
if (props.tags) {
return(
<section className={`${styles['tags']}`}>
{contentCover.tags.map((tag: Tag) => {
{props.tags.map((tag: Tag) => {
return(
<TagComponent
tag={tag}
Expand Down
6 changes: 0 additions & 6 deletions src/models/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ export interface ContentResponseWithFetchResponse {
body: Content
}

export interface ContentCover {
title: string,
tags?: Array<Tag>,
publishedAt: number,
}

export interface ContentMeta {
id: string,
robots: {
Expand Down
6 changes: 3 additions & 3 deletions src/utils/filterRequests.ts → src/utils/match.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { isMatch } from 'micromatch';
import { isMatch as _isMatch } from 'micromatch';
import { filterRequestPaths } from '../../config';

export function isIgnoreRequest(path: string) {
export function isMatch(path: string) {
if (!path || !filterRequestPaths || filterRequestPaths.length == 0) {
return false;
}
return isMatch(path, filterRequestPaths);
return _isMatch(path, filterRequestPaths);
}

0 comments on commit 57148d8

Please sign in to comment.