Skip to content

Commit

Permalink
fix: rename telefuncConfig to config
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Nov 7, 2022
1 parent b19e06c commit 01695da
Show file tree
Hide file tree
Showing 24 changed files with 70 additions and 65 deletions.
4 changes: 2 additions & 2 deletions docs/pages/disableNamingConvention.page.server.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Disable the telefunction naming convention, See <Link href="/event-based#naming-
```js
// Environment: Server

import { telefuncConfig } from 'telefunc'
import { config } from 'telefunc'

telefuncConfig.disableNamingConvention = true
config.disableNamingConvention = true
```
4 changes: 2 additions & 2 deletions docs/pages/event-based.page.server.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ We can use `disableNamingConvention` to disable the warnings:

```js
// server.js
import { telefuncConfig } from 'telefunc'
telefunc.disableNamingConvention = true
import { config } from 'telefunc'
config.disableNamingConvention = true
```

> First-time Telefunc users often create generic telefunctions out of the habit of using REST/GraphQL, but defining tailored telefunctions instead is usually the better appraoch as explained above.
Expand Down
4 changes: 2 additions & 2 deletions docs/pages/httpHeaders.page.server.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ Usually used for sending authentication headers.
```js
// Environment: Browser

import { telefuncConfig } from 'telefunc/client'
import { config } from 'telefunc/client'

telefuncConfig.httpHeaders = {
config.httpHeaders = {
Authorization: `Bearer ${token}`
}
```
8 changes: 4 additions & 4 deletions docs/pages/root.page.server.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { Link } from '@brillout/docpress'

**Environment**: Server.

We use `telefuncConfig.root` to tell Telefunc where our project's root directory lives.
We use `config.root` to tell Telefunc where our project's root directory lives.

```js
// Environment: Server

import { telefuncConfig } from 'telefunc'
import { config } from 'telefunc'

telefuncConfig.root = '/home/alice/code/my-app/'
config.root = '/home/alice/code/my-app/'
```

It's optional and is only needed if we use <Link text={<code>telefuncConfig.telefuncFiles</code>} href="/telefuncFiles" />.
It's optional and is only needed if we use <Link text={<code>config.telefuncFiles</code>} href="/telefuncFiles" />.
6 changes: 3 additions & 3 deletions docs/pages/telefuncFiles.page.server.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { Link, Note } from '@brillout/docpress'

<Note type="warning">This config is experimental and breaking changes can be introduced in any minor version update.</Note>

If our stack doesn't support <Link text="server-side transformation" href="/transformer" />, then we have to use `telefuncConfig.telefuncFiles` for providing Telefunc the list of `.telefunc.js` files.
If our stack doesn't support <Link text="server-side transformation" href="/transformer" />, then we have to use `config.telefuncFiles` for providing Telefunc the list of `.telefunc.js` files.

```js
// Environment: Server

import { telefuncConfig } from 'telefunc'
import { config } from 'telefunc'

telefuncConfig.telefuncFiles = [
config.telefuncFiles = [
require.resolve('./hello.telefunc.mjs')
]
```
10 changes: 5 additions & 5 deletions docs/pages/telefuncUrl.page.server.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ import { Link, Note } from '@brillout/docpress'

By default, Telefunc makes its HTTP requests by using the URL `/_telefunc`.

We can use `telefuncConfig.telefuncUrl` to change Telefunc's URL.
We can use `config.telefuncUrl` to change Telefunc's URL.

```js
// Environment: Server

import { telefuncConfig } from 'telefunc'
import { config } from 'telefunc'

telefuncConfig.telefuncUrl = '/api/_telefunc'
config.telefuncUrl = '/api/_telefunc'
```

```js
// Environment: Browser

import { telefuncConfig } from 'telefunc/client'
import { config } from 'telefunc/client'

telefuncConfig.telefuncUrl = '/api/_telefunc'
config.telefuncUrl = '/api/_telefunc'
```

<Note type="warning">Make sure to change the URL at both the Telefunc Client *and* the Telefunc Server.</Note>
6 changes: 3 additions & 3 deletions examples/babel/server.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import express from 'express'
import { telefunc, telefuncConfig } from 'telefunc'
import { telefunc, config } from 'telefunc'
import { createRequire } from 'module'

startServer()

telefuncConfig.disableNamingConvention = true
config.disableNamingConvention = true
{
const require = createRequire(import.meta.url)
telefuncConfig.telefuncFiles = [require.resolve('./hello.telefunc.mjs')]
config.telefuncFiles = [require.resolve('./hello.telefunc.mjs')]
}

async function startServer() {
Expand Down
4 changes: 2 additions & 2 deletions examples/cloudflare-workers/devServer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const express = require('express')
const { telefunc, telefuncConfig } = require('telefunc')
const { telefunc, config } = require('telefunc')

startServer()

Expand All @@ -18,7 +18,7 @@ function start(app) {

function installTelefunc(app) {
app.use(express.text())
telefuncConfig.disableNamingConvention = true
config.disableNamingConvention = true
app.all('/_telefunc', async (req, res) => {
const { originalUrl: url, method, body } = req
const httpResponse = await telefunc({ url, method, body })
Expand Down
4 changes: 2 additions & 2 deletions examples/cloudflare-workers/worker/telefunc.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { telefunc, telefuncConfig } from 'telefunc'
import { telefunc, config } from 'telefunc'

export { handleTelefunc }

telefuncConfig.disableNamingConvention = true
config.disableNamingConvention = true

async function handleTelefunc({ url, method, body }) {
const httpResponse = await telefunc({ url, method, body })
Expand Down
4 changes: 2 additions & 2 deletions examples/next/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export default MyApp

import { telefuncConfig } from 'telefunc/client'
import { config } from 'telefunc/client'
import type { AppProps } from 'next/app'
import React from 'react'

const isBrowser = typeof window !== 'undefined'
if (isBrowser) {
telefuncConfig.telefuncUrl = '/api/_telefunc'
config.telefuncUrl = '/api/_telefunc'
}

function MyApp({ Component, pageProps }: AppProps) {
Expand Down
4 changes: 2 additions & 2 deletions examples/next/pages/api/_telefunc.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { getUser } from '../../auth/getUser'
import { telefunc, telefuncConfig, provideTelefuncContext } from 'telefunc'
import { telefunc, config, provideTelefuncContext } from 'telefunc'
import type { NextApiRequest, NextApiResponse } from 'next'
import assert from 'assert'

telefuncConfig.telefuncUrl = '/api/_telefunc'
config.telefuncUrl = '/api/_telefunc'

export default async function telefuncMiddleware(req: NextApiRequest, res: NextApiResponse) {
const user = getUser(req)
Expand Down
4 changes: 2 additions & 2 deletions examples/react-native/App.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useEffect, useState } from 'react'
import { View, Text } from 'react-native'
import { hello } from './hello.telefunc.mjs'
import { telefuncConfig } from 'telefunc/client'
import { config } from 'telefunc/client'

telefuncConfig.telefuncUrl = 'http://localhost:3000/_telefunc'
config.telefuncUrl = 'http://localhost:3000/_telefunc'

export default function App() {
const [state, set] = useState({ isLoading: true })
Expand Down
6 changes: 3 additions & 3 deletions examples/react-native/server.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import express from 'express'
import cors from 'cors'
import { telefunc, telefuncConfig } from 'telefunc'
import { telefunc, config } from 'telefunc'
import { createRequire } from 'module'

startServer()

telefuncConfig.disableNamingConvention = true
config.disableNamingConvention = true
{
const require = createRequire(import.meta.url)
telefuncConfig.telefuncFiles = [require.resolve('./hello.telefunc.mjs')]
config.telefuncFiles = [require.resolve('./hello.telefunc.mjs')]
}

async function startServer() {
Expand Down
10 changes: 5 additions & 5 deletions telefunc/client/clientConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { configUser as telefuncConfig }
export { configUser as config }
export { resolveClientConfig }

import { assertUsage } from './utils'
Expand Down Expand Up @@ -26,21 +26,21 @@ function resolveClientConfig(): ConfigResolved {

function validateUserConfig(configUserUnwrapped: ConfigUser, prop: string, val: unknown) {
if (prop === 'telefuncUrl') {
assertUsage(typeof val === 'string', 'telefuncConfig.telefuncUrl should be a string')
assertUsage(typeof val === 'string', 'config.telefuncUrl should be a string')
const isIpAddress = (value: string) => /^\d/.test(value)
assertUsage(
val.startsWith('/') || val.startsWith('http') || isIpAddress(val),
`Setting \`telefuncConfig.telefuncUrl\` to \`${val}\` but it should be one of the following: a URL pathname (e.g. \`/_telefunc\`), a URL with origin (e.g. \`https://example.org/_telefunc\`), or an IP address (e.g. \`192.158.1.38\`).`
`Setting \`config.telefuncUrl\` to \`${val}\` but it should be one of the following: a URL pathname (e.g. \`/_telefunc\`), a URL with origin (e.g. \`https://example.org/_telefunc\`), or an IP address (e.g. \`192.158.1.38\`).`
)
configUserUnwrapped[prop] = val
} else if (prop === 'httpHeaders') {
assertUsage(
typeof val === 'object' && val !== null && Object.values(val).every((v) => typeof v === 'string'),
'`telefuncConfig.httpHeaders` should be an object of strings'
'`config.httpHeaders` should be an object of strings'
)
configUserUnwrapped[prop] = val as Record<string, string>
} else {
assertUsage(false, `Unknown telefuncConfig.${prop}`)
assertUsage(false, `Unknown config.${prop}`)
}

return true
Expand Down
4 changes: 3 additions & 1 deletion telefunc/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export { telefuncConfig } from './clientConfig'
import { config } from './clientConfig'
export { config }
export { config as telefuncConfig }
export { onAbort, onTelefunctionRemoteCallError } from './remoteTelefunctionCall/onAbort'
export type { TelefunctionError } from './TelefunctionError'

Expand Down
1 change: 1 addition & 0 deletions telefunc/node/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// no-op
export const telefuncConfig = {}
export const config = {}
export const onAbort = () => {}
/** @deprecated */
export const onTelefunctionRemoteCallError = () => {}
4 changes: 3 additions & 1 deletion telefunc/node/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export { telefunc } from './telefunc'
export { telefuncConfig } from './serverConfig'
import { config } from './serverConfig'
export { config }
export { config as telefuncConfig }
export { getContext, provideTelefuncContext } from './getContext'
export { Abort } from './Abort'
export { shield } from './shield'
Expand Down
2 changes: 1 addition & 1 deletion telefunc/node/server/runTelefunc/getEtag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async function getEtag(runContext: { disableEtag: boolean; httpResponseBody: str
/*
assertWarning(
false,
'The HTTP response ETag header missing because the Node.js module `crypto` could not be loaded. Set `telefuncConfig.disableEtag = true` to remove this warning.',
'The HTTP response ETag header missing because the Node.js module `crypto` could not be loaded. Set `config.disableEtag = true` to remove this warning.',
{ onlyOnce: true }
)
*/
Expand Down
2 changes: 1 addition & 1 deletion telefunc/node/server/runTelefunc/loadTelefuncFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async function loadTelefuncFiles(runContext: {
telefuncFilePath: string
}): Promise<{ telefuncFilesLoaded: TelefuncFiles; telefuncFilesAll: string[] }> {
// Handles:
// - When the user provides the telefunc file paths with `telefuncConfig.telefuncFiles`
// - When the user provides the telefunc file paths with `config.telefuncFiles`
{
if (runContext.telefuncFilesManuallyProvidedByUser) {
assert(hasProp(runContext, 'telefuncFilesManuallyProvidedByUser', 'string[]')) // Help TS narrow `runContext`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async function loadTelefuncFilesFromConfig(runContext: {
telefuncFilePath: string
}): Promise<{ telefuncFilesLoaded: TelefuncFiles; telefuncFilesAll: string[] }> {
const { appRootDir } = runContext
assertUsage(appRootDir, 'You need to set `telefuncConfig.root` to be able to use `telefuncConfig.telefuncFiles`')
assertUsage(appRootDir, 'You need to set `config.root` to be able to use `config.telefuncFiles`')
assertPosixPath(appRootDir)
const telefuncFilesLoaded: TelefuncFiles = {}
const telefuncFilesAll: string[] = []
Expand Down
2 changes: 1 addition & 1 deletion telefunc/node/server/runTelefunc/parseHttpRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function assertUrl(runContext: { httpRequest: { url: string }; serverConfig: { t
const urlPathname = getUrlPathname(runContext.httpRequest.url)
assertUsage(
urlPathname === runContext.serverConfig.telefuncUrl,
`telefunc({ url }): The pathname of \`url\` is \`${urlPathname}\` but it's expected to be \`${runContext.serverConfig.telefuncUrl}\`. Either make sure that \`url\` is the HTTP request URL, or set \`telefuncConfig.telefuncUrl\` to \`${urlPathname}\`.`
`telefunc({ url }): The pathname of \`url\` is \`${urlPathname}\` but it's expected to be \`${runContext.serverConfig.telefuncUrl}\`. Either make sure that \`url\` is the HTTP request URL, or set \`config.telefuncUrl\` to \`${urlPathname}\`.`
)
}

Expand Down
22 changes: 11 additions & 11 deletions telefunc/node/server/serverConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { configUser as telefuncConfig }
export { configUser as config }
export { resolveServerConfig }
export type { ConfigUser }

Expand Down Expand Up @@ -51,30 +51,30 @@ function resolveServerConfig(): ConfigResolved {

function validateUserConfig(configUserUnwrapped: ConfigUser, prop: string, val: unknown) {
if (prop === 'root') {
assertUsage(typeof val === 'string', 'telefuncConfig.root should be a string')
assertUsage(isAbsolute(val), 'telefuncConfig.root should be an absolute path')
assertUsage(typeof val === 'string', 'config.root should be a string')
assertUsage(isAbsolute(val), 'config.root should be an absolute path')
configUserUnwrapped[prop] = val
} else if (prop === 'telefuncUrl') {
assertUsage(typeof val === 'string', 'telefuncConfig.telefuncUrl should be a string')
assertUsage(val.startsWith('/'), "telefuncConfig.telefuncUrl should start with '/'")
assertUsage(typeof val === 'string', 'config.telefuncUrl should be a string')
assertUsage(val.startsWith('/'), "config.telefuncUrl should start with '/'")
configUserUnwrapped[prop] = val
} else if (prop === 'telefuncFiles') {
const wrongType = '`telefuncConfig.telefuncFiles` should be a list of paths'
const wrongType = '`config.telefuncFiles` should be a list of paths'
assertUsage(Array.isArray(val), wrongType)
val.forEach((val: unknown) => {
assertUsage(typeof val === 'string', wrongType)
assertUsage(isAbsolute(val), `[telefuncConfig.telefuncFiles] ${val} should be an absolute path`)
assertUsage(isTelefuncFilePath(val), `[telefuncConfig.telefuncFiles] ${val} doesn't contain \`.telefunc.\``)
assertUsage(isAbsolute(val), `[config.telefuncFiles] ${val} should be an absolute path`)
assertUsage(isTelefuncFilePath(val), `[config.telefuncFiles] ${val} doesn't contain \`.telefunc.\``)
})
configUserUnwrapped[prop] = val
} else if (prop === 'disableEtag') {
assertUsage(typeof val === 'boolean', 'telefuncConfig.disableEtag should be a boolean')
assertUsage(typeof val === 'boolean', 'config.disableEtag should be a boolean')
configUserUnwrapped[prop] = val
} else if (prop === 'disableNamingConvention') {
assertUsage(typeof val === 'boolean', '`telefuncConfig.disableNamingConvention` should be a boolean')
assertUsage(typeof val === 'boolean', '`config.disableNamingConvention` should be a boolean')
configUserUnwrapped[prop] = val
} else {
assertUsage(false, `Unknown telefuncConfig.${prop}`)
assertUsage(false, `Unknown config.${prop}`)
}

return true
Expand Down
4 changes: 2 additions & 2 deletions telefunc/node/vite/plugins/importBuild/loadBuild.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { loadTelefuncFilesWithImportBuild }
export { setLoaders }

import { telefuncConfig } from '../../../server/serverConfig'
import { config } from '../../../server/serverConfig'
import { getGlobalObject } from '../../utils'
import { assertManifest } from '../manifest/assertManifest'

Expand All @@ -26,7 +26,7 @@ function setLoaders({
function setServerConfig(loadManifest: LoadManifest) {
const manifest = loadManifest()
assertManifest(manifest)
Object.assign(telefuncConfig, manifest.config)
Object.assign(config, manifest.config)
}

async function loadTelefuncFilesWithImportBuild(): Promise<unknown> {
Expand Down
14 changes: 7 additions & 7 deletions telefunc/node/vite/plugins/manifest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ export { manifest }
export { manifestFileName }

import type { Plugin } from 'vite'
import { telefuncConfig, type ConfigUser } from '../../../server/serverConfig'
import { config, type ConfigUser } from '../../../server/serverConfig'
import { assert, viteIsSSR, projectInfo } from '../../utils'
import { assertManifest } from './assertManifest'

const manifestFileName = 'telefunc.json' as const

function manifest(config: ConfigUser = {}): Plugin {
function manifest(configUser: ConfigUser = {}): Plugin {
// - For dev
// - Ensures that `config` is valid before this.emitFile() writes `dist/server/telefunc.json` to disk
Object.assign(telefuncConfig, config)
// - Ensures that `configUser` is valid before this.emitFile() writes `dist/server/telefunc.json` to disk
Object.assign(config, configUser)

// For prod
let ssr: boolean | undefined
Expand All @@ -23,7 +23,7 @@ function manifest(config: ConfigUser = {}): Plugin {
if (!ssr) return
const manifest = {
version: projectInfo.projectVersion,
config
config: configUser
}
assertManifest(manifest)
this.emitFile({
Expand All @@ -32,8 +32,8 @@ function manifest(config: ConfigUser = {}): Plugin {
source: JSON.stringify(manifest, null, 2)
})
},
configResolved(config) {
ssr = viteIsSSR(config)
configResolved(viteConfig) {
ssr = viteIsSSR(viteConfig)
}
}
}

0 comments on commit 01695da

Please sign in to comment.