-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(docs/examples): add dynamic headers (#1065)
- Loading branch information
1 parent
f5a6476
commit 8d25612
Showing
22 changed files
with
160 additions
and
18 deletions.
There are no files selected for viewing
Empty file.
3 changes: 3 additions & 0 deletions
3
examples/transport-http_headers__dynamicHeaders.output-encoder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
examples/transport-http_headers__dynamicHeaders.output.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } }` }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
tests/examples/generated|generated_arguments__arguments.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
tests/examples/transport-http_headers__dynamicHeaders.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
1 change: 1 addition & 0 deletions
1
website/content/guides/_example_links/transport-http_headers.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |