Skip to content

Commit

Permalink
docs: fix playgrounds on Stackblitz (#39)
Browse files Browse the repository at this point in the history
* polyfill

* nextjs issues

* docs
  • Loading branch information
unnoq authored Nov 29, 2024
1 parent c210724 commit bd66bad
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 37 deletions.
2 changes: 1 addition & 1 deletion apps/content/content/docs/playground.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ featuring pre-configured examples accessible instantly via StackBlitz or local s

| Environment | StackBlitz | GitHub Source |
|------------|------------|---------------|
| Next.js Playground | [Local Only](https://stackblitz.com/github/unnoq/orpc/tree/main/playgrounds/nextjs?file=src%2Frouter%2Findex.ts) | [View Source](https://github.com/unnoq/orpc/tree/main/playgrounds/nextjs/src/app/page.tsx) |
| Next.js Playground | [Open in StackBlitz](https://stackblitz.com/github/unnoq/orpc/tree/main/playgrounds/nextjs?file=src%2Fapp%2Fpage.tsx) | [View Source](https://github.com/unnoq/orpc/tree/main/playgrounds/nextjs/src/app/page.tsx) |
| OpenAPI Playground | [Open in StackBlitz](https://stackblitz.com/github/unnoq/orpc/tree/main/playgrounds/openapi?file=src%2Frouter%2Findex.ts) | [View Source](https://github.com/unnoq/orpc/tree/main/playgrounds/openapi/src/router/index.ts) |
| OpenAPI with Contract Playground | [Open in StackBlitz](https://stackblitz.com/github/unnoq/orpc/tree/main/playgrounds/contract-openapi?file=src%2Frouter%2Findex.ts) | [View Source](https://github.com/unnoq/orpc/tree/main/playgrounds/contract-openapi/src/router/index.ts) |
| Express.js Playground | [Open in StackBlitz](https://stackblitz.com/github/unnoq/orpc/tree/main/playgrounds/expressjs?file=src%2Frouter%2Findex.ts) | [View Source](https://github.com/unnoq/orpc/tree/main/playgrounds/expressjs/src/router/index.ts) |
Expand Down
3 changes: 2 additions & 1 deletion playgrounds/contract-openapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"name": "orpc-contract-openapi-playground",
"type": "module",
"version": "0.0.0",
"packageManager": "pnpm@8.15.6",
"description": "oRPC Contract OpenAPI Playground",
"author": "",
"author": "unnoq",
"license": "ISC",
"keywords": ["orpc", "contract", "openapi", "playground", "unnoq"],
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions playgrounds/contract-openapi/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createFetchHandler } from '@orpc/server/fetch'
import { createServerAdapter } from '@whatwg-node/server'
import { contract } from './contract'
import { router } from './router'
import './polyfill'

const orpcHandler = createFetchHandler({
router,
Expand Down
21 changes: 21 additions & 0 deletions playgrounds/contract-openapi/src/polyfill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { File } from 'node:buffer'

/**
* This file aims to polyfill missing APIs in Node.js 18 that oRPC depends on.
*
* Since Stackblitz runs on Node.js 18, these polyfills ensure oRPC works in that environment.
* If you're running oRPC locally, please use Node.js 20 or later for full compatibility.
*/

/**
* Note: Stackblitz provides an emulated Node.js environment with inherent limitations.
* If you encounter issues, please test on a local setup with Node.js 20 or later before reporting them.
*/

/**
* The `oz.file()` schema depends on the `File` API.
* If you're not using `oz.file()`, you can safely remove this polyfill.
*/
if (typeof globalThis.File === 'undefined') {
globalThis.File = File as any
}
3 changes: 2 additions & 1 deletion playgrounds/expressjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"name": "orpc-expresjs-playground",
"type": "module",
"version": "0.0.0",
"packageManager": "pnpm@8.15.6",
"description": "oRPC ExpressJS Playground",
"author": "",
"author": "unnoq",
"license": "ISC",
"keywords": ["orpc", "expressjs", "playground", "unnoq"],
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions playgrounds/expressjs/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createFetchHandler } from '@orpc/server/fetch'
import { createServerAdapter } from '@whatwg-node/server'
import express from 'express'
import { router } from './router'
import './polyfill'

const app = express()

Expand Down
21 changes: 21 additions & 0 deletions playgrounds/expressjs/src/polyfill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { File } from 'node:buffer'

/**
* This file aims to polyfill missing APIs in Node.js 18 that oRPC depends on.
*
* Since Stackblitz runs on Node.js 18, these polyfills ensure oRPC works in that environment.
* If you're running oRPC locally, please use Node.js 20 or later for full compatibility.
*/

/**
* Note: Stackblitz provides an emulated Node.js environment with inherent limitations.
* If you encounter issues, please test on a local setup with Node.js 20 or later before reporting them.
*/

/**
* The `oz.file()` schema depends on the `File` API.
* If you're not using `oz.file()`, you can safely remove this polyfill.
*/
if (typeof globalThis.File === 'undefined') {
globalThis.File = File as any
}
2 changes: 2 additions & 0 deletions playgrounds/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"name": "orpc-nextjs-playground",
"version": "0.1.0",
"private": true,
"packageManager": "pnpm@8.15.6",
"description": "oRPC NextJS Playground",
"author": "unnoq",
"keywords": [
"orpc",
"nextjs",
Expand Down
8 changes: 3 additions & 5 deletions playgrounds/nextjs/src/app/api/[...rest]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ import { router } from './router'
const handler = createFetchHandler({
router,
serverless: true,
// hooks(context, hooks) {
// return hooks.next()
// },
})

function handleRequest(request: Request) {
const context = request.headers.get('Authorization')
? { user: { id: 'test', name: 'John Doe', email: 'john@doe.com' } }
: {}

return handler({
request,
prefix: '/api',
context,
})
}

Expand Down
61 changes: 33 additions & 28 deletions playgrounds/nextjs/src/orpc.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
import type { z } from 'zod'
import type { UserSchema } from './schemas/user'
import { ORPCError, os } from '@orpc/server'
import { cookies, headers } from 'next/headers'
import './polyfill'

const base = os.use(async (input, context, meta) => {
const headerList = await headers()
let user = headerList.get('Authorization')
? { id: 'test', name: 'John Doe', email: 'john@doe.com' } satisfies z.infer<typeof UserSchema>
: undefined
const base = os
.use(async (input, context, meta) => {
const start = Date.now()

if (!user) {
const cookieList = await cookies()
// do something with cookies
user = { id: 'test', name: 'John Doe', email: 'john@doe.com' }
}

return meta.next({
context: {
db: 'dummy:postgres',
user,
},
try {
return await meta.next({})
}
catch (e) {
console.error(e)
throw e
}
finally {
// eslint-disable-next-line no-console
console.log(`[${meta.path.join('/')}] ${Date.now() - start}ms`)
}
})
})
.use(async (input, context, meta) => {
// You can use headers or cookies here to create the user object:
// import { cookies, headers } from 'next/headers'
// const headerList = await headers();
// const cookieList = await cookies();
//
// These lines are commented out because Stackblitz has issues with Next.js headers and cookies.
// However, this works fine in a local environment.

export const pub = base.use(async (input, context, meta) => {
const start = Date.now()
const user = { id: 'test', name: 'John Doe', email: 'john@doe.com' } satisfies z.infer<typeof UserSchema>

try {
return await meta.next({})
}
finally {
// eslint-disable-next-line no-console
console.log(`[${meta.path.join('/')}] ${Date.now() - start}ms`)
}
})
return meta.next({
context: {
db: 'dummy:postgres',
user,
},
})
})

export const pub = base

export const authed = pub.use(async (input, context, meta) => {
if (!context.user) {
Expand Down
21 changes: 21 additions & 0 deletions playgrounds/nextjs/src/polyfill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { File } from 'node:buffer'

/**
* This file aims to polyfill missing APIs in Node.js 18 that oRPC depends on.
*
* Since Stackblitz runs on Node.js 18, these polyfills ensure oRPC works in that environment.
* If you're running oRPC locally, please use Node.js 20 or later for full compatibility.
*/

/**
* Note: Stackblitz provides an emulated Node.js environment with inherent limitations.
* If you encounter issues, please test on a local setup with Node.js 20 or later before reporting them.
*/

/**
* The `oz.file()` schema depends on the `File` API.
* If you're not using `oz.file()`, you can safely remove this polyfill.
*/
if (typeof globalThis.File === 'undefined') {
globalThis.File = File as any
}
3 changes: 2 additions & 1 deletion playgrounds/openapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"name": "orpc-openapi-playground",
"type": "module",
"version": "0.0.0",
"packageManager": "pnpm@8.15.6",
"description": "oRPC OpenAPI Playground",
"author": "",
"author": "unnoq",
"license": "ISC",
"keywords": ["orpc", "openapi", "playground", "unnoq"],
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions playgrounds/openapi/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { generateOpenAPI } from '@orpc/openapi'
import { createFetchHandler } from '@orpc/server/fetch'
import { createServerAdapter } from '@whatwg-node/server'
import { router } from './router'
import './polyfill'

const orpcHandler = createFetchHandler({
router,
Expand Down
21 changes: 21 additions & 0 deletions playgrounds/openapi/src/polyfill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { File } from 'node:buffer'

/**
* This file aims to polyfill missing APIs in Node.js 18 that oRPC depends on.
*
* Since Stackblitz runs on Node.js 18, these polyfills ensure oRPC works in that environment.
* If you're running oRPC locally, please use Node.js 20 or later for full compatibility.
*/

/**
* Note: Stackblitz provides an emulated Node.js environment with inherent limitations.
* If you encounter issues, please test on a local setup with Node.js 20 or later before reporting them.
*/

/**
* The `oz.file()` schema depends on the `File` API.
* If you're not using `oz.file()`, you can safely remove this polyfill.
*/
if (typeof globalThis.File === 'undefined') {
globalThis.File = File as any
}

0 comments on commit bd66bad

Please sign in to comment.