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