From c1a85e7aee9007615382e5319f5f8c288a035fdd Mon Sep 17 00:00:00 2001 From: Islam Shehata Date: Mon, 4 Jul 2022 14:58:17 +0200 Subject: [PATCH 1/6] adding with-axiom example --- examples/with-axiom/.gitignore | 34 ++++++++++++++++++++ examples/with-axiom/README.md | 44 ++++++++++++++++++++++++++ examples/with-axiom/middleware.js | 9 ++++++ examples/with-axiom/next.config.js | 8 +++++ examples/with-axiom/package.json | 15 +++++++++ examples/with-axiom/pages/_app.js | 11 +++++++ examples/with-axiom/pages/api/hello.js | 9 ++++++ examples/with-axiom/pages/index.js | 28 ++++++++++++++++ 8 files changed, 158 insertions(+) create mode 100644 examples/with-axiom/.gitignore create mode 100644 examples/with-axiom/README.md create mode 100644 examples/with-axiom/middleware.js create mode 100644 examples/with-axiom/next.config.js create mode 100644 examples/with-axiom/package.json create mode 100644 examples/with-axiom/pages/_app.js create mode 100644 examples/with-axiom/pages/api/hello.js create mode 100644 examples/with-axiom/pages/index.js diff --git a/examples/with-axiom/.gitignore b/examples/with-axiom/.gitignore new file mode 100644 index 000000000000..1437c53f70bc --- /dev/null +++ b/examples/with-axiom/.gitignore @@ -0,0 +1,34 @@ +# 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* + +# local env files +.env.local +.env.development.local +.env.test.local +.env.production.local + +# vercel +.vercel diff --git a/examples/with-axiom/README.md b/examples/with-axiom/README.md new file mode 100644 index 000000000000..8095f09547c0 --- /dev/null +++ b/examples/with-axiom/README.md @@ -0,0 +1,44 @@ +# Example app with Axiom + +This example shows how to use a [Next.js](https://nextjs.org/) project along with [Axiom](https://axiom.co) via the [next-axiom](https://github.com/axiomhq/next-axiom) package. A custom `withAxiom` wrapper is used to wrap the next config object, middleware and API functions. The `log` object could be used from frontend, middleware and API functions. + +## Deploy your own + +Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) or preview live with [StackBlitz](https://stackblitz.com/github/vercel/next.js/tree/canary/examples/with-axiom) + +[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-axiom&project-name=with-axiom&repository-name=with-axiom) + +Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details. + +## How to use + +First, run the development server: + +```bash +npm run dev +# or +yarn dev +``` + +Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. + +You can start editing the page by modifying `pages/index.js`. The page auto-updates as you edit the file. + +[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.js`. + +The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages. + + +## How to use + +Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), or [pnpm](https://pnpm.io) to bootstrap the example:: + +```bash +npx create-next-app --example with-axiom with-axiom-app +# or +yarn create next-app --example with-axiom with-axiom-app +# or +pnpm create next-app --example with-axiom with-axiom-app +``` + +Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)) and watch data coming into your Axiom dataset. diff --git a/examples/with-axiom/middleware.js b/examples/with-axiom/middleware.js new file mode 100644 index 000000000000..9ff1b31aab37 --- /dev/null +++ b/examples/with-axiom/middleware.js @@ -0,0 +1,9 @@ +import { NextResponse } from 'next/server' +import { log, withAxiom } from 'next-axiom' + +async function middleware(_req, ev) { + log.info("Hello from middleware", { 'bar': 'baz' }); + return NextResponse.next() +} + +export default withAxiom(middleware) \ No newline at end of file diff --git a/examples/with-axiom/next.config.js b/examples/with-axiom/next.config.js new file mode 100644 index 000000000000..e58acaf3cf2a --- /dev/null +++ b/examples/with-axiom/next.config.js @@ -0,0 +1,8 @@ +const { withAxiom } = require('next-axiom'); + +/** @type {import('next').NextConfig} */ +const nextConfig = withAxiom({ + reactStrictMode: true, +}) + +module.exports = nextConfig diff --git a/examples/with-axiom/package.json b/examples/with-axiom/package.json new file mode 100644 index 000000000000..9be5078a7e5e --- /dev/null +++ b/examples/with-axiom/package.json @@ -0,0 +1,15 @@ +{ + "private": true, + "scripts": { + "dev": "next dev", + "build": "next build", + "start": "next start" + }, + "dependencies": { + "next": "latest", + "next-axiom": "^0.10.0", + "react": "^18.0.0", + "react-dom": "^18.0.0", + "swr": "^1.3.0" + } +} \ No newline at end of file diff --git a/examples/with-axiom/pages/_app.js b/examples/with-axiom/pages/_app.js new file mode 100644 index 000000000000..2cae5cb85614 --- /dev/null +++ b/examples/with-axiom/pages/_app.js @@ -0,0 +1,11 @@ +import { log } from 'next-axiom' + +export { reportWebVitals } from 'next-axiom' + +log.info('Hello from frontend', { foo: 'bar' }) + +function MyApp({ Component, pageProps }) { + return +} + +export default MyApp diff --git a/examples/with-axiom/pages/api/hello.js b/examples/with-axiom/pages/api/hello.js new file mode 100644 index 000000000000..ab84f1479a28 --- /dev/null +++ b/examples/with-axiom/pages/api/hello.js @@ -0,0 +1,9 @@ +// Next.js API route support: https://nextjs.org/docs/api-routes/introduction +import { log, withAxiom } from 'next-axiom' + +async function handler(req, res) { + log.info('Hello from function', { url: req.url }); + res.status(200).json({ name: 'John Doe' }) +} + +export default withAxiom(handler) diff --git a/examples/with-axiom/pages/index.js b/examples/with-axiom/pages/index.js new file mode 100644 index 000000000000..3deccad91552 --- /dev/null +++ b/examples/with-axiom/pages/index.js @@ -0,0 +1,28 @@ +import { log } from 'next-axiom' +import useSWR from 'swr' + +export async function getStaticProps(context) { + log.info('Hello from SSR', { context }) + return { + props: {}, + } +} + +const fetcher = async (...args) => { + log.info('Hello from SWR', { args }); + const res = await fetch(...args); + return await res.json(); +} + +export default function Home() { + const { data, error } = useSWR('/api/hello', fetcher) + + if (error) return
Failed to load
+ if (!data) return
Loading...
+ + return ( +
+

{data.name}

+
+ ) +} From 0705302647857107711b80fda05727c504f17619 Mon Sep 17 00:00:00 2001 From: Islam Shehata Date: Thu, 7 Jul 2022 13:31:02 +0200 Subject: [PATCH 2/6] convert with-axiom to typescript --- examples/with-axiom/middleware.js | 9 --------- examples/with-axiom/middleware.ts | 9 +++++++++ examples/with-axiom/package.json | 6 ++++++ examples/with-axiom/pages/{_app.js => _app.tsx} | 6 ++++-- .../with-axiom/pages/api/{hello.js => hello.ts} | 5 +++-- examples/with-axiom/pages/{index.js => index.tsx} | 13 ++++++++----- 6 files changed, 30 insertions(+), 18 deletions(-) delete mode 100644 examples/with-axiom/middleware.js create mode 100644 examples/with-axiom/middleware.ts rename examples/with-axiom/pages/{_app.js => _app.tsx} (63%) rename examples/with-axiom/pages/api/{hello.js => hello.ts} (55%) rename examples/with-axiom/pages/{index.js => index.tsx} (56%) diff --git a/examples/with-axiom/middleware.js b/examples/with-axiom/middleware.js deleted file mode 100644 index 9ff1b31aab37..000000000000 --- a/examples/with-axiom/middleware.js +++ /dev/null @@ -1,9 +0,0 @@ -import { NextResponse } from 'next/server' -import { log, withAxiom } from 'next-axiom' - -async function middleware(_req, ev) { - log.info("Hello from middleware", { 'bar': 'baz' }); - return NextResponse.next() -} - -export default withAxiom(middleware) \ No newline at end of file diff --git a/examples/with-axiom/middleware.ts b/examples/with-axiom/middleware.ts new file mode 100644 index 000000000000..415b117e4fdf --- /dev/null +++ b/examples/with-axiom/middleware.ts @@ -0,0 +1,9 @@ +import { NextResponse, NextRequest } from 'next/server' +import { log, withAxiom } from 'next-axiom' + +async function middleware(_req: NextRequest) { + log.info("Hello from middleware", { 'bar': 'baz' }) + return NextResponse.next() +} + +export default withAxiom(middleware) diff --git a/examples/with-axiom/package.json b/examples/with-axiom/package.json index 9be5078a7e5e..f68957eb498a 100644 --- a/examples/with-axiom/package.json +++ b/examples/with-axiom/package.json @@ -11,5 +11,11 @@ "react": "^18.0.0", "react-dom": "^18.0.0", "swr": "^1.3.0" + }, + "devDependencies": { + "@types/react": "^18.0.5", + "@types/react-dom": "^18.0.1", + "@types/node": "^16.11.26", + "typescript": "^4.6.3" } } \ No newline at end of file diff --git a/examples/with-axiom/pages/_app.js b/examples/with-axiom/pages/_app.tsx similarity index 63% rename from examples/with-axiom/pages/_app.js rename to examples/with-axiom/pages/_app.tsx index 2cae5cb85614..11ce65c35bdb 100644 --- a/examples/with-axiom/pages/_app.js +++ b/examples/with-axiom/pages/_app.tsx @@ -1,10 +1,12 @@ import { log } from 'next-axiom' - +import { AppProps } from 'next/app' export { reportWebVitals } from 'next-axiom' log.info('Hello from frontend', { foo: 'bar' }) -function MyApp({ Component, pageProps }) { +type props = {} + +const MyApp = ({ Component, pageProps }: AppProps) => { return } diff --git a/examples/with-axiom/pages/api/hello.js b/examples/with-axiom/pages/api/hello.ts similarity index 55% rename from examples/with-axiom/pages/api/hello.js rename to examples/with-axiom/pages/api/hello.ts index ab84f1479a28..8e3e71efc832 100644 --- a/examples/with-axiom/pages/api/hello.js +++ b/examples/with-axiom/pages/api/hello.ts @@ -1,7 +1,8 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction -import { log, withAxiom } from 'next-axiom' +import type { NextApiRequest, NextApiResponse } from 'next' +import { log, withAxiom } from 'next-axiom'; -async function handler(req, res) { +async function handler(req: NextApiRequest, res: NextApiResponse) { log.info('Hello from function', { url: req.url }); res.status(200).json({ name: 'John Doe' }) } diff --git a/examples/with-axiom/pages/index.js b/examples/with-axiom/pages/index.tsx similarity index 56% rename from examples/with-axiom/pages/index.js rename to examples/with-axiom/pages/index.tsx index 3deccad91552..8c7cfec4c098 100644 --- a/examples/with-axiom/pages/index.js +++ b/examples/with-axiom/pages/index.tsx @@ -1,20 +1,21 @@ +import { GetStaticPropsContext } from 'next' import { log } from 'next-axiom' import useSWR from 'swr' -export async function getStaticProps(context) { - log.info('Hello from SSR', { context }) +export const getStaticProps = async (ctx: GetStaticPropsContext) => { + log.info('Hello from SSR', { ctx }) return { props: {}, } } -const fetcher = async (...args) => { +const fetcher = async (...args: any[]) => { log.info('Hello from SWR', { args }); - const res = await fetch(...args); + const res = await fetch.apply(null, [...args]); return await res.json(); } -export default function Home() { +const Home = () => { const { data, error } = useSWR('/api/hello', fetcher) if (error) return
Failed to load
@@ -26,3 +27,5 @@ export default function Home() { ) } + +export default Home From a36011b45d1b7aa353c8a68dc40794c8a5fe0b45 Mon Sep 17 00:00:00 2001 From: Islam Shehata Date: Tue, 12 Jul 2022 10:16:26 +0200 Subject: [PATCH 3/6] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Balázs Orbán --- examples/with-axiom/.gitignore | 3 +++ examples/with-axiom/middleware.ts | 4 ++-- examples/with-axiom/package.json | 6 +++--- examples/with-axiom/pages/_app.tsx | 2 -- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/examples/with-axiom/.gitignore b/examples/with-axiom/.gitignore index 1437c53f70bc..88b6f0d98164 100644 --- a/examples/with-axiom/.gitignore +++ b/examples/with-axiom/.gitignore @@ -32,3 +32,6 @@ yarn-error.log* # vercel .vercel + +# typescript +*.tsbuildinfo diff --git a/examples/with-axiom/middleware.ts b/examples/with-axiom/middleware.ts index 415b117e4fdf..1aae279cf1ab 100644 --- a/examples/with-axiom/middleware.ts +++ b/examples/with-axiom/middleware.ts @@ -1,7 +1,7 @@ -import { NextResponse, NextRequest } from 'next/server' +import { NextResponse } from 'next/server' import { log, withAxiom } from 'next-axiom' -async function middleware(_req: NextRequest) { +async function middleware() { log.info("Hello from middleware", { 'bar': 'baz' }) return NextResponse.next() } diff --git a/examples/with-axiom/package.json b/examples/with-axiom/package.json index f68957eb498a..1cbc2fb4731d 100644 --- a/examples/with-axiom/package.json +++ b/examples/with-axiom/package.json @@ -8,14 +8,14 @@ "dependencies": { "next": "latest", "next-axiom": "^0.10.0", - "react": "^18.0.0", - "react-dom": "^18.0.0", + "react": "^18.2.0", + "react-dom": "^18.2.0", "swr": "^1.3.0" }, "devDependencies": { "@types/react": "^18.0.5", "@types/react-dom": "^18.0.1", "@types/node": "^16.11.26", - "typescript": "^4.6.3" + "typescript": "^4.7.4" } } \ No newline at end of file diff --git a/examples/with-axiom/pages/_app.tsx b/examples/with-axiom/pages/_app.tsx index 11ce65c35bdb..27fd9af82530 100644 --- a/examples/with-axiom/pages/_app.tsx +++ b/examples/with-axiom/pages/_app.tsx @@ -4,8 +4,6 @@ export { reportWebVitals } from 'next-axiom' log.info('Hello from frontend', { foo: 'bar' }) -type props = {} - const MyApp = ({ Component, pageProps }: AppProps) => { return } From 345190767b5dc179cf552d8eec1b804347bf00ba Mon Sep 17 00:00:00 2001 From: Islam Shehata Date: Tue, 12 Jul 2022 10:29:18 +0200 Subject: [PATCH 4/6] remove duplicate how-to-use section --- examples/with-axiom/README.md | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/examples/with-axiom/README.md b/examples/with-axiom/README.md index 8095f09547c0..0671cfa9c5fa 100644 --- a/examples/with-axiom/README.md +++ b/examples/with-axiom/README.md @@ -10,25 +10,6 @@ Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_mediu Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details. -## How to use - -First, run the development server: - -```bash -npm run dev -# or -yarn dev -``` - -Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. - -You can start editing the page by modifying `pages/index.js`. The page auto-updates as you edit the file. - -[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.js`. - -The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages. - - ## How to use Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), or [pnpm](https://pnpm.io) to bootstrap the example:: From 6cec52ac3dd1a0c81e6c5eeeb927898af29d259d Mon Sep 17 00:00:00 2001 From: Islam Shehata Date: Tue, 12 Jul 2022 10:35:23 +0200 Subject: [PATCH 5/6] add tsconfig.json --- examples/with-axiom/tsconfig.json | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 examples/with-axiom/tsconfig.json diff --git a/examples/with-axiom/tsconfig.json b/examples/with-axiom/tsconfig.json new file mode 100644 index 000000000000..84f59a7aed86 --- /dev/null +++ b/examples/with-axiom/tsconfig.json @@ -0,0 +1,29 @@ +{ + "compilerOptions": { + "target": "es5", + "lib": [ + "dom", + "dom.iterable", + "esnext" + ], + "allowJs": true, + "skipLibCheck": true, + "strict": false, + "forceConsistentCasingInFileNames": true, + "noEmit": true, + "incremental": true, + "esModuleInterop": true, + "module": "esnext", + "resolveJsonModule": true, + "isolatedModules": true, + "jsx": "preserve" + }, + "include": [ + "next-env.d.ts", + "**/*.ts", + "**/*.tsx" + ], + "exclude": [ + "node_modules" + ] +} From 37b51f14c476086f63bec46cad6c09b8e32f63d0 Mon Sep 17 00:00:00 2001 From: Islam Shehata Date: Thu, 1 Sep 2022 15:39:38 +0200 Subject: [PATCH 6/6] fix linting --- examples/with-axiom/middleware.ts | 2 +- examples/with-axiom/next-env.d.ts | 5 +++++ examples/with-axiom/next.config.js | 2 +- examples/with-axiom/package.json | 2 +- examples/with-axiom/pages/api/hello.ts | 4 ++-- examples/with-axiom/pages/index.tsx | 6 +++--- examples/with-axiom/tsconfig.json | 16 +++------------- 7 files changed, 16 insertions(+), 21 deletions(-) create mode 100644 examples/with-axiom/next-env.d.ts diff --git a/examples/with-axiom/middleware.ts b/examples/with-axiom/middleware.ts index 1aae279cf1ab..9c2833a5b8ec 100644 --- a/examples/with-axiom/middleware.ts +++ b/examples/with-axiom/middleware.ts @@ -2,7 +2,7 @@ import { NextResponse } from 'next/server' import { log, withAxiom } from 'next-axiom' async function middleware() { - log.info("Hello from middleware", { 'bar': 'baz' }) + log.info('Hello from middleware', { bar: 'baz' }) return NextResponse.next() } diff --git a/examples/with-axiom/next-env.d.ts b/examples/with-axiom/next-env.d.ts new file mode 100644 index 000000000000..4f11a03dc6cc --- /dev/null +++ b/examples/with-axiom/next-env.d.ts @@ -0,0 +1,5 @@ +/// +/// + +// NOTE: This file should not be edited +// see https://nextjs.org/docs/basic-features/typescript for more information. diff --git a/examples/with-axiom/next.config.js b/examples/with-axiom/next.config.js index e58acaf3cf2a..4ddf957b071a 100644 --- a/examples/with-axiom/next.config.js +++ b/examples/with-axiom/next.config.js @@ -1,4 +1,4 @@ -const { withAxiom } = require('next-axiom'); +const { withAxiom } = require('next-axiom') /** @type {import('next').NextConfig} */ const nextConfig = withAxiom({ diff --git a/examples/with-axiom/package.json b/examples/with-axiom/package.json index 1cbc2fb4731d..5c1b7401446c 100644 --- a/examples/with-axiom/package.json +++ b/examples/with-axiom/package.json @@ -18,4 +18,4 @@ "@types/node": "^16.11.26", "typescript": "^4.7.4" } -} \ No newline at end of file +} diff --git a/examples/with-axiom/pages/api/hello.ts b/examples/with-axiom/pages/api/hello.ts index 8e3e71efc832..3515f82ab103 100644 --- a/examples/with-axiom/pages/api/hello.ts +++ b/examples/with-axiom/pages/api/hello.ts @@ -1,9 +1,9 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction import type { NextApiRequest, NextApiResponse } from 'next' -import { log, withAxiom } from 'next-axiom'; +import { log, withAxiom } from 'next-axiom' async function handler(req: NextApiRequest, res: NextApiResponse) { - log.info('Hello from function', { url: req.url }); + log.info('Hello from function', { url: req.url }) res.status(200).json({ name: 'John Doe' }) } diff --git a/examples/with-axiom/pages/index.tsx b/examples/with-axiom/pages/index.tsx index 8c7cfec4c098..230ac54de96a 100644 --- a/examples/with-axiom/pages/index.tsx +++ b/examples/with-axiom/pages/index.tsx @@ -10,9 +10,9 @@ export const getStaticProps = async (ctx: GetStaticPropsContext) => { } const fetcher = async (...args: any[]) => { - log.info('Hello from SWR', { args }); - const res = await fetch.apply(null, [...args]); - return await res.json(); + log.info('Hello from SWR', { args }) + const res = await fetch.apply(null, [...args]) + return await res.json() } const Home = () => { diff --git a/examples/with-axiom/tsconfig.json b/examples/with-axiom/tsconfig.json index 84f59a7aed86..b9cc80c0d4ec 100644 --- a/examples/with-axiom/tsconfig.json +++ b/examples/with-axiom/tsconfig.json @@ -1,11 +1,7 @@ { "compilerOptions": { "target": "es5", - "lib": [ - "dom", - "dom.iterable", - "esnext" - ], + "lib": ["dom", "dom.iterable", "esnext"], "allowJs": true, "skipLibCheck": true, "strict": false, @@ -18,12 +14,6 @@ "isolatedModules": true, "jsx": "preserve" }, - "include": [ - "next-env.d.ts", - "**/*.ts", - "**/*.tsx" - ], - "exclude": [ - "node_modules" - ] + "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"], + "exclude": ["node_modules"] }