Skip to content

Commit

Permalink
Added support for BigInt to API routes (#10215)
Browse files Browse the repository at this point in the history
* Added support for bigint to API routes

Closes #7980

* added BigInt test to get result

Co-authored-by: Joe Haddad <timer150@gmail.com>
  • Loading branch information
jamesmosier and Timer committed Jan 27, 2020
1 parent 569e7ac commit c9dc17b
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/next/build/babel/preset.ts
Expand Up @@ -177,6 +177,7 @@ module.exports = (
],
require('@babel/plugin-proposal-optional-chaining'),
require('@babel/plugin-proposal-nullish-coalescing-operator'),
isServer && require('@babel/plugin-syntax-bigint'),
].filter(Boolean),
}
}
1 change: 1 addition & 0 deletions packages/next/package.json
Expand Up @@ -63,6 +63,7 @@
"@babel/plugin-proposal-nullish-coalescing-operator": "7.7.4",
"@babel/plugin-proposal-object-rest-spread": "7.6.2",
"@babel/plugin-proposal-optional-chaining": "7.7.4",
"@babel/plugin-syntax-bigint": "7.8.3",
"@babel/plugin-syntax-dynamic-import": "7.2.0",
"@babel/plugin-transform-modules-commonjs": "7.7.0",
"@babel/plugin-transform-runtime": "7.6.2",
Expand Down
4 changes: 4 additions & 0 deletions test/integration/bigint/pages/api/bigint.js
@@ -0,0 +1,4 @@
export default (req, res) => {
res.statusCode = 200
res.send((1n + 2n).toString())
}
77 changes: 77 additions & 0 deletions test/integration/bigint/test/index.test.js
@@ -0,0 +1,77 @@
/* eslint-env jest */
/* global jasmine */
import fs from 'fs-extra'
import { join } from 'path'
import {
fetchViaHTTP,
launchApp,
nextBuild,
nextStart,
killApp,
findPort,
} from 'next-test-utils'

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2

const appDir = join(__dirname, '..')
const nextConfig = join(appDir, 'next.config.js')
let appPort
let app

const runTests = () => {
it('should return 200', async () => {
const res = await fetchViaHTTP(appPort, '/api/bigint', null, {
method: 'GET',
})
expect(res.status).toEqual(200)
})

it('should return the BigInt result text', async () => {
const resText = await fetchViaHTTP(appPort, '/api/bigint', null, {
method: 'GET',
}).then(res => res.ok && res.text())
expect(resText).toEqual('3')
})
}

describe('bigint API route support', () => {
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))

runTests()
})

describe('server mode', () => {
beforeAll(async () => {
await fs.remove(nextConfig)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))

runTests()
})

describe('serverless mode', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = { target: 'serverless' }`
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
await fs.remove(nextConfig)
})

runTests()
})
})
12 changes: 12 additions & 0 deletions yarn.lock
Expand Up @@ -220,6 +220,11 @@
resolved "https://registry.yarnpkg.com/@babel/helper-plugin-utils/-/helper-plugin-utils-7.0.0.tgz#bbb3fbee98661c569034237cc03967ba99b4f250"
integrity sha512-CYAOUCARwExnEixLdB6sDm2dIJ/YgEAKDM1MOeMeZu9Ld/bDgVo8aiWrXwcY7OBh+1Ea2uUcVRcxKk0GJvW7QA==

"@babel/helper-plugin-utils@^7.8.0":
version "7.8.3"
resolved "https://registry.npmjs.org/@babel/helper-plugin-utils/-/helper-plugin-utils-7.8.3.tgz#9ea293be19babc0f52ff8ca88b34c3611b208670"
integrity sha512-j+fq49Xds2smCUNYmEHF9kGNkhbet6yVIBp4e6oeQpH1RUs/Ir06xUKzDjDkGcaaokPiTNs2JBWHjaE4csUkZQ==

"@babel/helper-regex@^7.0.0", "@babel/helper-regex@^7.4.4":
version "7.5.5"
resolved "https://registry.yarnpkg.com/@babel/helper-regex/-/helper-regex-7.5.5.tgz#0aa6824f7100a2e0e89c1527c23936c152cab351"
Expand Down Expand Up @@ -384,6 +389,13 @@
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"

"@babel/plugin-syntax-bigint@7.8.3":
version "7.8.3"
resolved "https://registry.npmjs.org/@babel/plugin-syntax-bigint/-/plugin-syntax-bigint-7.8.3.tgz#4c9a6f669f5d0cdf1b90a1671e9a146be5300cea"
integrity sha512-wnTnFlG+YxQm3vDxpGE57Pj0srRU4sHE/mDkt1qv2YJJSeUAec2ma4WLUnUPeKjyrfntVwe/N6dCXpU+zL3Npg==
dependencies:
"@babel/helper-plugin-utils" "^7.8.0"

"@babel/plugin-syntax-dynamic-import@7.2.0":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-dynamic-import/-/plugin-syntax-dynamic-import-7.2.0.tgz#69c159ffaf4998122161ad8ebc5e6d1f55df8612"
Expand Down

0 comments on commit c9dc17b

Please sign in to comment.