Step 11 - Initialise common
Course: Blogging website - chapter 11
- Create a new folder called
commonand initialize an empty ts project in it
mkdir commoncd commonnpm init -ynpx tsc --init- Update
tsconfig.json
"rootDir": "./src","outDir": "./dist","declaration": true,- Sign up/login to npmjs.org
- Run
npm login - Update the
nameinpackage.jsonto be in your own npm namespace, Update main to bedist/index.js
{ "name": "@100xdevs/common-app", "version": "1.0.0", "description": "", "main": "dist/index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC"}- Add
srcto.npmignore - Install zod
npm i zod- Put all types in
src/index.ts - signupInput / SignupInput
- signinInput / SigninInput
- createPostInput / CreatePostInput
- updatePostInput / UpdatePostInput
Solution
import z from "zod";
export const signupInput = z.object({ email: z.string().email(), password: z.string(), name: z.string().optional(),});
export type SignupType = z.infer<typeof signupInput>;
export const signinInput = z.object({ email: z.string().email(), password: z.string(),});
export type SigninType = z.infer<typeof signinInput>;
export const createPostInput = z.object({ title: z.string(), content: z.string(),});
export type CreatePostType = z.infer<typeof createPostInput>;
export const updatePostInput = z.object({ title: z.string().optional(), content: z.string().optional(),});
export type UpdatePostType = z.infer<typeof updatePostInput>;tsc -bto generate the output- Publish to npm
npm publish --access public- Explore your package on npmjs