Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions nextjs-ts/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
40 changes: 40 additions & 0 deletions nextjs-ts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo

# ChiselStrike
.chiseld-data.db*
.chiseld.db*
.gen
4 changes: 4 additions & 0 deletions nextjs-ts/Chisel.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
models = ["models"]
endpoints = ["endpoints"]
policies = ["policies"]
modules = "node"
32 changes: 32 additions & 0 deletions nextjs-ts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# ChiselStrike with Next.js TypeScript Example

This example showcases using Next.js and ChiselStrike with TypeScript.

## Getting Started

First, clone the files locally:

```bash
npx degit chiselstrike/chiselstrike-examples/nextjs-ts my-app
```

Go to the project directory:

```bash
cd my-app
```

Install dependencies:

```bash
npm install
```

Start the development server:

```bash
npm run dev
```

This should start Next.js and the ChiselStrike server at the same time. The Next.js server
should be accessible in [http://localhost:3000](http://localhost:3000), whereas ChiselStrike server should be accessible in [http://localhost:8080](http://localhost:8080).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, but what do I do then? Which elements of `http://localhost:3000] demonstrate ChiselStrike and how do I interact with them?

14 changes: 14 additions & 0 deletions nextjs-ts/endpoints/create-people.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ChiselRequest, responseFromJson } from "@chiselstrike/api";

import { Person } from "../models/person.model";

export default async function chisel(req: ChiselRequest) {
if (req.method == "POST") {
const payload = await req.json();
const firstName = payload["firstName"];
const lastName = payload["lastName"];
const created = await Person.create({ firstName, lastName });
return responseFromJson(created);
}
return responseFromJson("Only POST is allowed", 405);
}
16 changes: 16 additions & 0 deletions nextjs-ts/endpoints/get-all-people.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ChiselRequest, responseFromJson } from "@chiselstrike/api";

import { Person } from "../models/person.model";

export default async function chisel(req: ChiselRequest) {
if (req.method == "GET") {
try {
let resp_json: Person[] = [];
await Person.cursor().forEach((p) => resp_json.push(p));
return responseFromJson(resp_json);
} catch (e) {
return responseFromJson(e, 500);
}
}
return responseFromJson("Only GET is allowed", 405);
}
21 changes: 21 additions & 0 deletions nextjs-ts/lib/withSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { withIronSessionApiRoute, withIronSessionSsr } from "iron-session/next";
import { NextApiHandler } from "next";

const sessionOptions = {
password: "complex_password_at_least_32_characters_long",
cookieName: "chiselstrike_cookie10",
// secure: true should be used in production (HTTPS) but can't be used in development (HTTP)
cookieOptions: {
secure: process.env.NODE_ENV === "production",
},
};

export function withSessionRoute(handler: NextApiHandler) {
return withIronSessionApiRoute(handler, sessionOptions);
}

export function withSessionSsr(
handler: Parameters<typeof withIronSessionSsr>[0]
) {
return withIronSessionSsr(handler, sessionOptions);
}
6 changes: 6 additions & 0 deletions nextjs-ts/models/person.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ChiselEntity } from "@chiselstrike/api";

export class Person extends ChiselEntity {
firstName: string = "";
lastName: string = "";
}
5 changes: 5 additions & 0 deletions nextjs-ts/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
6 changes: 6 additions & 0 deletions nextjs-ts/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
}

module.exports = nextConfig
Loading