Skip to content

Commit

Permalink
chore(docs/examples): add dynamic headers (#1065)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Sep 5, 2024
1 parent f5a6476 commit 8d25612
Show file tree
Hide file tree
Showing 22 changed files with 160 additions and 18 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const encode = (value: string) => {
return value.replace(/'x-sent-at-time': '\d+'/g, `'x-sent-at-time': 'DYNAMIC_VALUE'`)
}
10 changes: 10 additions & 0 deletions examples/transport-http_headers__dynamicHeaders.output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
url: 'https://countries.trevorblades.com/graphql',
body: '{"query":"{ languages { code } }"}',
method: 'POST',
headers: Headers {
accept: 'application/graphql-response+json; charset=utf-8, application/json; charset=utf-8',
'content-type': 'application/json',
'x-sent-at-time': 'DYNAMIC_VALUE'
}
}
23 changes: 23 additions & 0 deletions examples/transport-http_headers__dynamicHeaders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Graffle } from '../src/entrypoints/main.js'
import { publicGraphQLSchemaEndpoints, show } from './$helpers.js'

const graffle = Graffle
.create({
schema: publicGraphQLSchemaEndpoints.SocialStudies,
})
.use(async ({ pack }) => {
return await pack({
input: {
...pack.input,
headers: {
'X-Sent-At-Time': Date.now().toString(),
},
},
})
})
.use(async ({ exchange }) => {
show(exchange.input.request)
return exchange()
})

await graffle.rawString({ document: `{ languages { code } }` })
22 changes: 17 additions & 5 deletions scripts/generate-examples-derivatives/generate-tests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { globby } from 'globby'
import * as FS from 'node:fs/promises'
import * as Path from 'node:path'
import { deleteFiles } from '../lib/deleteFiles.js'
import { readFiles } from '../lib/readFiles.js'

Expand All @@ -8,32 +10,42 @@ export const generateTests = async () => {

const files = await readFiles({
pattern: `./examples/*.ts`,
options: { ignore: [`./examples/$*`] },
options: { ignore: [`./examples/$*`, `./examples/*.output.*`, `./examples/*.output-encoder.*`] },
})
const encoderFilePaths = await globby(`./examples/*.output-encoder.ts`)

const outputDir = Path.join(process.cwd(), `./tests/examples`)

await Promise.all(files.map(async (file) => {
const encoderFilePath = encoderFilePaths.find((encoderFilePath) =>
encoderFilePath.match(new RegExp(`${file.name}.output-encoder.ts`)) !== null
)
const code = `// @vitest-environment node
// WARNING:
// This test is generated by scripts/generate-test-examples.ts
// This test is generated by scripts/generate-example-derivatives/generate.ts
// Do not modify this file directly.
import { execaCommand } from 'execa'
import stripAnsi from 'strip-ansi'
import { expect, test } from 'vitest'
import { expect, test } from 'vitest'${
encoderFilePath
? `\nimport { encode } from '${Path.relative(outputDir, encoderFilePath.replace(`.ts`, `.js`))}'`
: ``
}
test(\`${file.name}\`, async () => {
const result = await execaCommand(\`pnpm tsx ./examples/${file.name}.ts\`)
expect(result.exitCode).toBe(0)
// Examples should output their data results.
const exampleResult = stripAnsi(result.stdout)
const exampleResult = ${encoderFilePath ? `encode(stripAnsi(result.stdout))` : `stripAnsi(result.stdout)`}
// If ever outputs vary by Node version, you can use this to snapshot by Node version.
// const nodeMajor = process.version.match(/v(\\d+)/)?.[1] ?? \`unknown\`
await expect(exampleResult).toMatchFileSnapshot(\`../../${file.path.dir}/${file.name}.output.txt\`)
})
`

await FS.writeFile(`./tests/examples/${file.name}.test.ts`, code)
await FS.writeFile(Path.join(outputDir, `${file.name}.test.ts`), code)
}))

console.log(`Generated a test for each example.`)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @vitest-environment node

// WARNING:
// This test is generated by scripts/generate-test-examples.ts
// This test is generated by scripts/generate-example-derivatives/generate.ts
// Do not modify this file directly.

import { execaCommand } from 'execa'
Expand Down
2 changes: 1 addition & 1 deletion tests/examples/raw.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @vitest-environment node

// WARNING:
// This test is generated by scripts/generate-test-examples.ts
// This test is generated by scripts/generate-example-derivatives/generate.ts
// Do not modify this file directly.

import { execaCommand } from 'execa'
Expand Down
2 changes: 1 addition & 1 deletion tests/examples/raw_rawString__rawString.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @vitest-environment node

// WARNING:
// This test is generated by scripts/generate-test-examples.ts
// This test is generated by scripts/generate-example-derivatives/generate.ts
// Do not modify this file directly.

import { execaCommand } from 'execa'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @vitest-environment node

// WARNING:
// This test is generated by scripts/generate-test-examples.ts
// This test is generated by scripts/generate-example-derivatives/generate.ts
// Do not modify this file directly.

import { execaCommand } from 'execa'
Expand Down
2 changes: 1 addition & 1 deletion tests/examples/raw_rawTyped__raw-typed.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @vitest-environment node

// WARNING:
// This test is generated by scripts/generate-test-examples.ts
// This test is generated by scripts/generate-example-derivatives/generate.ts
// Do not modify this file directly.

import { execaCommand } from 'execa'
Expand Down
2 changes: 1 addition & 1 deletion tests/examples/transport-http_RequestInput.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @vitest-environment node

// WARNING:
// This test is generated by scripts/generate-test-examples.ts
// This test is generated by scripts/generate-example-derivatives/generate.ts
// Do not modify this file directly.

import { execaCommand } from 'execa'
Expand Down
2 changes: 1 addition & 1 deletion tests/examples/transport-http_abort.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @vitest-environment node

// WARNING:
// This test is generated by scripts/generate-test-examples.ts
// This test is generated by scripts/generate-example-derivatives/generate.ts
// Do not modify this file directly.

import { execaCommand } from 'execa'
Expand Down
2 changes: 1 addition & 1 deletion tests/examples/transport-http_fetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @vitest-environment node

// WARNING:
// This test is generated by scripts/generate-test-examples.ts
// This test is generated by scripts/generate-example-derivatives/generate.ts
// Do not modify this file directly.

import { execaCommand } from 'execa'
Expand Down
20 changes: 20 additions & 0 deletions tests/examples/transport-http_headers__dynamicHeaders.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// @vitest-environment node

// WARNING:
// This test is generated by scripts/generate-example-derivatives/generate.ts
// Do not modify this file directly.

import { execaCommand } from 'execa'
import stripAnsi from 'strip-ansi'
import { expect, test } from 'vitest'
import { encode } from '../../examples/transport-http_headers__dynamicHeaders.output-encoder.js'

test(`transport-http_headers__dynamicHeaders`, async () => {
const result = await execaCommand(`pnpm tsx ./examples/transport-http_headers__dynamicHeaders.ts`)
expect(result.exitCode).toBe(0)
// Examples should output their data results.
const exampleResult = encode(stripAnsi(result.stdout))
// If ever outputs vary by Node version, you can use this to snapshot by Node version.
// const nodeMajor = process.version.match(/v(\d+)/)?.[1] ?? `unknown`
await expect(exampleResult).toMatchFileSnapshot(`../.././examples/transport-http_headers__dynamicHeaders.output.txt`)
})
2 changes: 1 addition & 1 deletion tests/examples/transport-memory.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @vitest-environment node

// WARNING:
// This test is generated by scripts/generate-test-examples.ts
// This test is generated by scripts/generate-example-derivatives/generate.ts
// Do not modify this file directly.

import { execaCommand } from 'execa'
Expand Down
8 changes: 8 additions & 0 deletions website/.vitepress/configExamples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ export const sidebarExamples: DefaultTheme.SidebarItem[] = [
'text': 'Transport Memory',
'link': '/examples/transport-memory',
},
{
'text': 'DynamicHeaders',
'link': '/examples/dynamicHeaders',
},
{
'text': 'DynamicHeaders.output Encoder',
'link': '/examples/dynamicHeaders.output-encoder',
},
{
'text': 'Transport Http Fetch',
'link': '/examples/transport-http_fetch',
Expand Down
45 changes: 45 additions & 0 deletions website/content/examples/dynamicHeaders.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
aside: false
---

# DynamicHeaders

```ts twoslash
import { Graffle } from 'graffle'

const graffle = Graffle
.create({
schema: `https://countries.trevorblades.com/graphql`,
})
.use(async ({ pack }) => {
return await pack({
input: {
...pack.input,
headers: {
'X-Sent-At-Time': Date.now().toString(),
},
},
})
})
.use(async ({ exchange }) => {
console.log(exchange.input.request)
return exchange()
})

await graffle.rawString({ document: `{ languages { code } }` })
```

#### Output

```txt
{
url: 'https://countries.trevorblades.com/graphql',
body: '{"query":"{ languages { code } }"}',
method: 'POST',
headers: Headers {
accept: 'application/graphql-response+json; charset=utf-8, application/json; charset=utf-8',
'content-type': 'application/json',
'x-sent-at-time': '1725556925194'
}
}
```
19 changes: 19 additions & 0 deletions website/content/examples/dynamicHeaders.output-encoder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
aside: false
---

# DynamicHeaders.output Encoder

```ts twoslash
export const encode = (snapshot: string) => {
return snapshot.replace(
/x-sent-at-time: '\d+'/,
`x-sent-at-time: 'DYNAMIC_VALUE'`,
)
}
```

#### Output

```txt
```
6 changes: 3 additions & 3 deletions website/content/examples/transport-http_RequestInput.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ await graffle.rawString({ document: `{ languages { code } }` })
body: '{"query":"{ languages { code } }"}',
method: 'POST',
headers: Headers {
authorization: 'Bearer MY_TOKEN',
accept: 'application/graphql-response+json',
'content-type': 'application/json'
accept: 'application/graphql-response+json; charset=utf-8, application/json; charset=utf-8',
'content-type': 'application/json',
authorization: 'Bearer MY_TOKEN'
},
mode: 'cors'
}
Expand Down
1 change: 1 addition & 0 deletions website/content/guides/_example_links/headers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
###### Examples -> [DynamicHeaders.output Encoder](../../examples/dynamicHeaders.output-encoder.md) / [DynamicHeaders](../../examples/dynamicHeaders.md)
2 changes: 1 addition & 1 deletion website/content/guides/_example_links/transport-http.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
###### Examples -> [Transport Http RequestInput](../../examples/transport-http_RequestInput.md) / [Transport Http Abort](../../examples/transport-http_abort.md) / [Transport Http Fetch](../../examples/transport-http_fetch.md)
###### Examples -> [Transport Http RequestInput](../../examples/transport-http_RequestInput.md) / [Transport Http Abort](../../examples/transport-http_abort.md) / [Transport Http Fetch](../../examples/transport-http_fetch.md) / [DynamicHeaders.output Encoder](../../examples/dynamicHeaders.output-encoder.md) / [DynamicHeaders](../../examples/dynamicHeaders.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
###### Examples -> [DynamicHeaders.output Encoder](../../examples/dynamicHeaders.output-encoder.md) / [DynamicHeaders](../../examples/dynamicHeaders.md)

0 comments on commit 8d25612

Please sign in to comment.