-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[with-typescript] Updated TypeScript example to use API routes
Next.js 9.0.0 has been out for a while, which supports API routes, but the examples were never updated to make use of it. This PR adds a simple example of an API route which also makes use of dynamic routing. A simple `fetch()` wrapper is also added for example purposes, and the pages structure have also been updated to dynamic routing.
- Loading branch information
Showing
9 changed files
with
77 additions
and
49 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
import { sampleUserData } from '../../../utils/sample-data' | ||
|
||
export default (req: NextApiRequest, res: NextApiResponse) => { | ||
try { | ||
const { id } = req.query; | ||
const selected = sampleUserData.find(data => data.id === Number(id)) | ||
|
||
if (!selected) { | ||
throw new Error('Cannot find user') | ||
} | ||
|
||
res.status(200).json(selected) | ||
} catch (err) { | ||
res.status(404).json({ statusCode: 404, message: err.message }) | ||
} | ||
} | ||
|
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,14 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
import { sampleUserData } from '../../../utils/sample-data' | ||
|
||
export default (_: NextApiRequest, res: NextApiResponse) => { | ||
try { | ||
if (!Array.isArray(sampleUserData)) { | ||
throw new Error('Cannot find user data') | ||
} | ||
|
||
res.status(200).json(sampleUserData) | ||
} catch (err) { | ||
res.status(500).json({ statusCode: 500, message: err.message }) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,34 +1,13 @@ | ||
import { User } from '../interfaces' | ||
|
||
/** Dummy user data. */ | ||
export const dataArray: User[] = [ | ||
{ id: 101, name: 'Alice' }, | ||
{ id: 102, name: 'Bob' }, | ||
{ id: 103, name: 'Caroline' }, | ||
{ id: 104, name: 'Dave' }, | ||
] | ||
|
||
/** | ||
* Calls a mock API which finds a user by ID from the list above. | ||
* | ||
* Throws an error if not found. | ||
*/ | ||
export async function findData(id: number | string) { | ||
const selected = dataArray.find(data => data.id === Number(id)) | ||
|
||
if (!selected) { | ||
throw new Error('Cannot find user') | ||
} | ||
|
||
return selected | ||
} | ||
|
||
/** Calls a mock API which returns the above array to simulate "get all". */ | ||
export async function findAll() { | ||
// Throw an error, just for example. | ||
if (!Array.isArray(dataArray)) { | ||
throw new Error('Cannot find users') | ||
import fetch from 'isomorphic-unfetch' | ||
|
||
export async function sampleFetchWrapper( | ||
input: RequestInfo, | ||
init?: RequestInit | ||
) { | ||
try { | ||
const data = await fetch(input, init).then(res => res.json()) | ||
return data | ||
} catch (err) { | ||
throw new Error(err.message) | ||
} | ||
|
||
return dataArray | ||
} |
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,9 @@ | ||
import { User } from '../interfaces' | ||
|
||
/** Dummy user data. */ | ||
export const sampleUserData: User[] = [ | ||
{ id: 101, name: 'Alice' }, | ||
{ id: 102, name: 'Bob' }, | ||
{ id: 103, name: 'Caroline' }, | ||
{ id: 104, name: 'Dave' }, | ||
] |