[RFC] API Routes in Expo Router (#24429)# Why Servers are an important part of developing many different types of apps, but they're much harder to configure than they need to be. API Routes
[RFC] API Routes in Expo Router (#24429)# Why Servers are an important part of developing many different types of apps, but they're much harder to configure than they need to be. API Routes will enable users to express some abstract JavaScript code that runs in a server by simply creating a file in the app directory, and adding the `+api.js` suffix. For example, to securely interact with OpenAI, simply: ```ts // app/generate+api.ts import { ExpoRequest, ExpoResponse } from 'expo-router/server'; export async function POST(req: ExpoRequest): Promise<ExpoResponse> { const { prompt } = await req.json(); const json = await fetch('https://api.openai.com/v1/engines/text-davinci-003/completions', { headers: { 'Content-Type': 'application/json', // `OPENAI_API_KEY` is pulled from the .env file when running in Expo CLI. Authorization: `Bearer ${process.env.OPENAI_API_KEY ?? ''}`, }, method: 'POST', body: JSON.stringify({ prompt, max_tokens: 100, }), }).then(res => res.json()); // Return as JSON return ExpoResponse.json(json); } ``` This will be served at `http://localhost:8081/generate` with `npx expo` and can be used by making a request: ```sh $ curl -X POST -H "Content-Type: application/json" -d \'{"prompt":"Hello, my name is"}\' http://localhost:8081/generate ``` Expo Router polyfills the URL and `window.location` object on native to allow for universally requesting with a relative URL: ```js // Expo prepends the host and port to the URL automatically in development. const json = await fetch('/generate').then(res => res.json()); ``` # How - API Routes are bundled with Metro, leveraging all the same functionality as the rest of the app and website. - The project babel config is used to transpile the API routes. Indication is passed to the Babel caller via the `isServer` boolean. This can be used to change the preset based on the environment. - Each API route is bundled into a standalone file in the `dist/_expo` directory. This is akin to ncc, the tool we use to make Create Expo App download in ~1 second. - Create a new package `@expo/server` which includes the requisite middleware and runtime polyfills for the Expo server environment. - Add a new routes manifest which will be used by `@expo/server` to serve up the three types of routes: HTML routes, API routes, and not found routes (404s). - Add a new export `expo-router/server` (potentially will be moved to `expo/server`) which contains the `ExpoRequest` and `ExpoResponse` objects. These are all based on the WinterCG specification, and include some additional properties for interop with the Expo Router filesystem convention. These are inspired by Remix, SvelteKit, and Next.js for simplicity. - Add a new export mode `web.output: "server"` which can be used to export a dynamic server. Note: I may drop this for now and make server the default since there's no expo-specific hosting code that must be exported. - This PR adds the ability to host the app with an express server, different production adapters to follow. # Test Plan In addition to all the E2E Metro tests, I've added a new E2E runner which starts a server and pings different requests to ensure expected behavior. These run in the CLI as opposed to the `@expo/server` package. - resolve ENG-10057 ENG-8243 ENG-8082 ENG-8079 ENG-8242 ENG-8081 ENG-8080 ENG-9625 --------- Co-authored-by: Expo Bot <[email protected]> Co-authored-by: Cedric van Putten <[email protected]>
show more ...
feat(docs): use custom layout icon for Expo Router Layout Routes (#22927)# Why - Help visually distinguish Layout Routes from other files. The `_layout` file format may be the first exposure th
feat(docs): use custom layout icon for Expo Router Layout Routes (#22927)# Why - Help visually distinguish Layout Routes from other files. The `_layout` file format may be the first exposure that many native developers get to magic file naming. <img width="984" alt="Screenshot 2023-06-15 at 6 35 26 PM" src="https://github.com/expo/expo/assets/9664363/5c33108c-d9da-4037-8dbf-a4967dde9588">
[docs] cleanup and unify snippet components (#22495)
[docs] Add custom builds config schema (#22165)Co-authored-by: Dominik Sokal <[email protected]>
[docs] improve snipped code clean copy regex (#22084)
[docs] migrate to new styleguide packages (#21557)
[docs] update Styleguide, remove font overwrites (#21188)
[notifications] autogenerate package documentation (#21002)
[docs] tweaks related to the new color palette (#20818)
[docs] Add radix colors (#20397)# Why I updated the styleguide to use Radix colors. This PR adds the new version of styleguide to the docs and changes all the color, shadow, and border-radius
[docs] Add radix colors (#20397)# Why I updated the styleguide to use Radix colors. This PR adds the new version of styleguide to the docs and changes all the color, shadow, and border-radius values. # How - Very carefully finding + replacing values. - Also used `tsc` to find spots where I missed. # Test Plan I looked through the docs for awhile and fixed up a lot of spots that did not look correct. However, there may be more spots out there, so please click through the website and report any color issues that appear in this PR and not in production. # Checklist <!-- Please check the appropriate items below if they apply to your diff. This is required for changes to Expo modules. --> - [x] Documentation is up to date to reflect these changes (eg: https://docs.expo.dev and README.md). - [x] Conforms with the [Documentation Writing Style Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md) - [x] This diff will work correctly for `expo prebuild` & EAS Build (eg: updated a module plugin).
[docs] switch to the new font system introduced in Next 13 (#20536)
[docs] remove old header components, fix minor appearance regression (#20469)
[docs] old text and lists components removal (#20442)
[docs] use mostly new components to render MDX files, unify links appearance (#20429)
[docs] fix code block annotations for bash/shell examples (#20300)
[docs] Variants: update headers, formatting and snippets (#20185)
[docs] improve Expo Config reference searchability (#19818)
[docs] update to Next 13, refactor links (#19698)
[docs] Snippet: improve actions accessibility, refactor SnackInline (#19389)
[docs] fix spacing inside LI elements, fix FCM guide appearance (#19368)
[docs] add ability to specify title of code block in MD files (#19246)
[docs] implement new Callouts, support type passing in MDs (#19238)
[docs] fix code annotation formatting (#19243)
[docs] convert app to ESM, update MDX to v2 (#18947)Co-authored-by: Jon Samp <[email protected]>
[docs] adjust typography and text elements spacing (#18837)Co-authored-by: Jon Samp <[email protected]>
12345