Skip to content

Commit

Permalink
docs: update function names
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Feb 1, 2022
1 parent d42a46f commit cf53c87
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
4 changes: 2 additions & 2 deletions docs/headings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ export const headings: HeadingDefinition[] = [
},
{
level: 2,
title: '`setContext()`',
url: '/setContext',
title: '`provideTelefuncContext()`',
url: '/provideTelefuncContext',
},
{
level: 4,
Expand Down
6 changes: 3 additions & 3 deletions docs/pages/auth.page.mdx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
TODO

We use `getContext()` with `setContext()` to pass contextual information to telefunctions,
We use `getContext()` with `provideTelefuncContext()` in order to pass contextual information to telefunctions,
such as the authenticated user's ID.

```js
// server.js
// Environment: Node.js

import { setContext } from 'telefunc'
import { provideTelefuncContext } from 'telefunc'

// Server middleware (Express.js/Fastify/Koa/Hapi/...)
app.use('*', async (req, res) => {
Expand All @@ -19,7 +19,7 @@ app.use('*', async (req, res) => {
const user = await authProviderApi.getUser(req.headers)

// We make `user` available to our telefunctions
setContext({ user })
provideTelefuncContext({ user })
})
```

Expand Down
File renamed without changes.
44 changes: 24 additions & 20 deletions docs/pages/tour.page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -348,13 +348,13 @@ Not only does `shield()` call `throw Abort()` on our behalf, but it also infers

Telefunctions often need context. Is the user logged-in? What is the user's ID? Etc.

We use `setContext()` and `getContext()` to provide contextual information to our telefunctions.
We use `getContext()` with `provideTelefuncContext()` in order to provide contextual information to our telefunctions.

```js
// server.js
// Environment: Node.js

import { setContext } from 'telefunc'
import { provideTelefuncContext } from 'telefunc'

// Server middleware (Express.js/Fastify/Koa/Hapi/...)
app.use('*', async (req, res) => {
Expand All @@ -363,7 +363,7 @@ app.use('*', async (req, res) => {
const user = req.user

// We make `user` available to our telefunctions
setContext({ user })
provideTelefuncContext({ user })
})
```

Expand Down Expand Up @@ -581,32 +581,36 @@ This inversion of control leads to a fundamentally improved separation of concer
## TypeScript
`shield()` infers the arguments' type:
`shield()` infers the argument types:
```ts
// UserSettings.telefunc.js
// Environment: Node.js

export { onUserSettingsChange }

import { shield } from 'telefunc'
const t = shield.type

shield(onUserSettingsChange, [
t.union(
{ name: t.value('firstName'), newVal: t.string },
{ name: t.value('age'), newVal: t.number }
)
])
export async function onUserSettingChange({ name, newVal }) {
if (name === 'firstName') {
// `shield()` infers `onUserSettingsChange`'s arguments type:
// TypeScript knows that `newVal` is a string in this if-block.
newVal++ // ❌ TypeScript complains that `newVal` is not a number.
}
if (name === 'age') {
// TypeScript knows that `newVal` is a number in this if-block.
newVal++ //
const onUserSettingsChange = shield(
[
t.or(
{ name: t.const('firstName'), newVal: t.string },
{ name: t.const('age'), newVal: t.number }
)
],
async ({ name, newVal }) => {
if (name === 'firstName') {
// The `onUserSettingsChange` argument types are inferred:
// TypeScript knows that `newVal` is a string in this if-block.
newVal++ // ❌ TypeScript complains that `newVal` is not a number.
}
if (name === 'age') {
// TypeScript knows that `newVal` is a number in this if-block.
newVal++ //
}
}
}
)
```
Our frontend seamlessly uses our backend telefunctions' type:
Expand Down

0 comments on commit cf53c87

Please sign in to comment.