refactor: renames, rewrite some file, function names and texts
This commit is contained in:
@@ -6,7 +6,7 @@ import {
|
||||
STATUS_OK,
|
||||
STATUS_UNAUTHORIZED
|
||||
} from '@utils/statusCodes';
|
||||
import { singleNews, topNews } from '@utils/urls';
|
||||
import { getSingleNews, getTopNews } from '@utils/urls';
|
||||
import { NewsDatabaseSchema, NewsDatabaseType } from '@utils/validationSchemas';
|
||||
import { Resend } from 'resend';
|
||||
|
||||
@@ -18,7 +18,7 @@ export async function GET(request: Request) {
|
||||
}
|
||||
|
||||
try {
|
||||
const topStories: number[] = await fetch(topNews, {
|
||||
const topStories: number[] = await fetch(getTopNews, {
|
||||
cache: 'no-store'
|
||||
}).then(res => res.json());
|
||||
|
||||
@@ -26,31 +26,33 @@ export async function GET(request: Request) {
|
||||
|
||||
const newsPromises = topStories
|
||||
.slice(0, Number(process.env.NEWS_LIMIT))
|
||||
.map(id => fetch(singleNews(id)).then(res => res.json()));
|
||||
.map(id => fetch(getSingleNews(id)).then(res => res.json()));
|
||||
|
||||
const news: NewsDatabaseType[] = await Promise.all(newsPromises);
|
||||
|
||||
const upsertPromises = news.map(async singleNews => {
|
||||
const validation = NewsDatabaseSchema.safeParse(singleNews);
|
||||
const upsertPromises = news.map(async getSingleNews => {
|
||||
const validation = NewsDatabaseSchema.safeParse(getSingleNews);
|
||||
|
||||
if (validation.success) {
|
||||
console.info(
|
||||
`Validated news N° ${singleNews.id} - ${singleNews.title}`
|
||||
`Validated news N° ${getSingleNews.id} - ${getSingleNews.title}`
|
||||
);
|
||||
const result = await prisma.news.upsert({
|
||||
create: {
|
||||
...validation.data,
|
||||
id: singleNews.id
|
||||
id: getSingleNews.id
|
||||
},
|
||||
update: {
|
||||
...validation.data
|
||||
},
|
||||
where: {
|
||||
id: singleNews.id
|
||||
id: getSingleNews.id
|
||||
}
|
||||
});
|
||||
|
||||
console.info(`Imported N° ${singleNews.id} - ${singleNews.title}`);
|
||||
console.info(
|
||||
`Imported N° ${getSingleNews.id} - ${getSingleNews.title}`
|
||||
);
|
||||
|
||||
return result;
|
||||
} else {
|
||||
@@ -69,7 +71,7 @@ export async function GET(request: Request) {
|
||||
from: process.env.RESEND_FROM,
|
||||
to: [process.env.ADMIN_EMAIL],
|
||||
subject: 'Newsletter: import cron job',
|
||||
text: `Found these ids ${topStories.join(', ')} and imported ${result.length} of them.`
|
||||
text: `Imported ${result.length} news out of these ids: ${topStories.join(', ')}.`
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -72,7 +72,7 @@ export async function GET(request: Request) {
|
||||
from: process.env.RESEND_FROM,
|
||||
to: [process.env.ADMIN_EMAIL],
|
||||
subject: 'Newsletter: mailing cron job',
|
||||
text: `Found ${users.length} users and ${news.length} news to send.`
|
||||
text: `Found ${users.length} users and ${news.length} news to send for ${new Date().toISOString()}.`
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -13,8 +13,6 @@ import { ResponseType, SubscribeFormSchema } from '@utils/validationSchemas';
|
||||
import * as crypto from 'crypto';
|
||||
import { Resend } from 'resend';
|
||||
|
||||
export const dynamic = 'force-dynamic'; // defaults to force-static
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
if (!process.env.RESEND_KEY || !process.env.RESEND_AUDIENCE) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import Tiles from '@components/tiles/Tiles';
|
||||
import { cn } from '@utils/ui';
|
||||
import { cn } from '@utils/cn';
|
||||
import { Analytics } from '@vercel/analytics/react';
|
||||
import type { Metadata } from 'next';
|
||||
import { Inter as FontSans } from 'next/font/google';
|
||||
|
||||
@@ -4,7 +4,7 @@ export default function manifest(): MetadataRoute.Manifest {
|
||||
return {
|
||||
name: 'Newsletter',
|
||||
short_name: 'Newsletter',
|
||||
description: 'Newsletter with Hackernews top stories',
|
||||
description: 'Newsletter with top stories from the Hacker News forum',
|
||||
start_url: '/',
|
||||
display: 'standalone',
|
||||
background_color: '#fff',
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Slot } from '@radix-ui/react-slot';
|
||||
import { cn } from '@utils/ui';
|
||||
import { cn } from '@utils/cn';
|
||||
import * as React from 'react';
|
||||
|
||||
export type ButtonProps = {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { cn } from '@utils/ui';
|
||||
import { cn } from '@utils/cn';
|
||||
import * as React from 'react';
|
||||
|
||||
const Card = React.forwardRef<
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { cn } from '@utils/ui';
|
||||
import { cn } from '@utils/cn';
|
||||
import * as React from 'react';
|
||||
|
||||
const Input = React.forwardRef<
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
'use client';
|
||||
|
||||
import * as LabelPrimitive from '@radix-ui/react-label';
|
||||
import { cn } from '@utils/ui';
|
||||
import { cn } from '@utils/cn';
|
||||
import { cva, type VariantProps } from 'class-variance-authority';
|
||||
import * as React from 'react';
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { sayings } from '@utils/sayings';
|
||||
import { getSayings } from '@utils/getSayings';
|
||||
import { textTruncate } from '@utils/textTruncate';
|
||||
import { NewsType } from '@utils/validationSchemas';
|
||||
import Template from './Template';
|
||||
@@ -9,7 +9,7 @@ export default function NewsletterTemplate(stories: NewsType[]) {
|
||||
template: (
|
||||
<Template
|
||||
title={`Here is something
|
||||
${sayings[Math.floor(Math.random() * sayings.length)]}!`}
|
||||
${getSayings[Math.floor(Math.random() * getSayings.length)]}!`}
|
||||
body={
|
||||
<div>
|
||||
{stories.map(story => {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useFormField } from '@hooks/useFormField';
|
||||
import { cn } from '@utils/ui';
|
||||
import { cn } from '@utils/cn';
|
||||
import * as React from 'react';
|
||||
|
||||
export const FormDescription = React.forwardRef<
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useFormField } from '@hooks/useFormField';
|
||||
import * as LabelPrimitive from '@radix-ui/react-label';
|
||||
import { cn } from '@utils/ui';
|
||||
import { cn } from '@utils/cn';
|
||||
import * as React from 'react';
|
||||
import { Label } from '../Label';
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useFormField } from '@hooks/useFormField';
|
||||
import { cn } from '@utils/ui';
|
||||
import { cn } from '@utils/cn';
|
||||
import * as React from 'react';
|
||||
|
||||
export const FormMessage = React.forwardRef<
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { cn } from '@utils/ui';
|
||||
import { cn } from '@utils/cn';
|
||||
import * as React from 'react';
|
||||
import { FormItemContext } from './FormItemContext';
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "nextjs-hackernews",
|
||||
"version": "0.3.0",
|
||||
"description": "Template for NodeJS APIs with TypeScript, with configurations for linting and testing",
|
||||
"version": "0.4.0",
|
||||
"description": "Newsletter with the top stories from the Hacker News forum",
|
||||
"author": "riccardo@frompixels.com",
|
||||
"scripts": {
|
||||
"dev": "next dev",
|
||||
@@ -20,7 +20,6 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@hookform/resolvers": "^3.3.2",
|
||||
"@next/third-parties": "^14.2.3",
|
||||
"@prisma/client": "^5.6.0",
|
||||
"@radix-ui/react-label": "^2.0.2",
|
||||
"@radix-ui/react-slot": "^1.0.2",
|
||||
|
||||
@@ -28,5 +28,5 @@
|
||||
}
|
||||
},
|
||||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
|
||||
"exclude": ["node_modules", ".yarn", ".next", ".vercel"]
|
||||
"exclude": ["node_modules", ".yarn", ".next", ".vercel", ".vscode"]
|
||||
}
|
||||
|
||||
6
utils/cn.ts
Normal file
6
utils/cn.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { type ClassValue, clsx } from 'clsx';
|
||||
import { twMerge } from 'tailwind-merge';
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs));
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
export const sayings = [
|
||||
export const getSayings = [
|
||||
'hot off the press',
|
||||
'straight from the oven',
|
||||
"straight from the horse's mouth",
|
||||
@@ -1,6 +0,0 @@
|
||||
import { type ClassValue, clsx } from "clsx"
|
||||
import { twMerge } from "tailwind-merge"
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs))
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
export const topNews = 'https://hacker-news.firebaseio.com/v0/topstories.json';
|
||||
export const getTopNews =
|
||||
'https://hacker-news.firebaseio.com/v0/topstories.json';
|
||||
|
||||
export function singleNews(id: number) {
|
||||
export function getSingleNews(id: number) {
|
||||
return `https://hacker-news.firebaseio.com/v0/item/${id}.json`;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export const ResponseSchema = z.object({
|
||||
success: z.boolean(),
|
||||
message: z.string()
|
||||
});
|
||||
|
||||
export type ResponseType = z.infer<typeof ResponseSchema>;
|
||||
export interface ResponseType {
|
||||
success: boolean;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export const SubscribeFormSchema = z.object({
|
||||
email: z.string().email()
|
||||
@@ -36,24 +34,17 @@ export const NewsDatabaseSchema = z.object({
|
||||
|
||||
export type NewsDatabaseType = z.infer<typeof NewsDatabaseSchema>;
|
||||
|
||||
export const NewsSchema = z.object({
|
||||
id: z.number(),
|
||||
title: z.string(),
|
||||
text: z.string().nullable(),
|
||||
type: z.string(),
|
||||
by: z.string(),
|
||||
time: z.number(),
|
||||
url: z.string().nullable(),
|
||||
score: z.number(),
|
||||
createdAt: z.date()
|
||||
});
|
||||
export interface NewsTileType {
|
||||
id: number;
|
||||
title: string;
|
||||
by: string;
|
||||
}
|
||||
|
||||
export type NewsType = z.infer<typeof NewsSchema>;
|
||||
|
||||
export const NewsTileSchema = z.object({
|
||||
id: z.number(),
|
||||
title: z.string(),
|
||||
by: z.string()
|
||||
});
|
||||
|
||||
export type NewsTileType = z.infer<typeof NewsTileSchema>;
|
||||
export interface NewsType extends NewsTileType {
|
||||
text: string | null;
|
||||
type: string;
|
||||
time: number;
|
||||
url: string | null;
|
||||
score: number;
|
||||
createdAt: Date;
|
||||
}
|
||||
|
||||
20
yarn.lock
20
yarn.lock
@@ -469,18 +469,6 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@next/third-parties@npm:^14.2.3":
|
||||
version: 14.2.3
|
||||
resolution: "@next/third-parties@npm:14.2.3"
|
||||
dependencies:
|
||||
third-party-capital: "npm:1.0.20"
|
||||
peerDependencies:
|
||||
next: ^13.0.0 || ^14.0.0
|
||||
react: ^18.2.0
|
||||
checksum: 3e6a5166601f3e25b113dcc8baeff2663526c73c5969b537b8174b881620e636ae4fbb9ac57fc359d978211991302280fb276168f8039e84d81df0e1fa990f09
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@nodelib/fs.scandir@npm:2.1.5":
|
||||
version: 2.1.5
|
||||
resolution: "@nodelib/fs.scandir@npm:2.1.5"
|
||||
@@ -4172,7 +4160,6 @@ __metadata:
|
||||
"@commitlint/cli": "npm:^18.4.3"
|
||||
"@commitlint/config-conventional": "npm:^18.4.3"
|
||||
"@hookform/resolvers": "npm:^3.3.2"
|
||||
"@next/third-parties": "npm:^14.2.3"
|
||||
"@prisma/client": "npm:^5.6.0"
|
||||
"@radix-ui/react-label": "npm:^2.0.2"
|
||||
"@radix-ui/react-slot": "npm:^1.0.2"
|
||||
@@ -5797,13 +5784,6 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"third-party-capital@npm:1.0.20":
|
||||
version: 1.0.20
|
||||
resolution: "third-party-capital@npm:1.0.20"
|
||||
checksum: 40e2531b428a21f05531fc62ef82859c0071211eff09588baf75d4b525096bbdb4f93e17e2d538849db8d4fc3cceb074933d7be59fddc26f86718fbbe852a97f
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"through2@npm:^4.0.0":
|
||||
version: 4.0.2
|
||||
resolution: "through2@npm:4.0.2"
|
||||
|
||||
Reference in New Issue
Block a user