🐛 fix: error thrown when using exposed server function with HTTP POST by edysegura · Pull Request #334 · nullstack/nullstack · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions server/server.js
2 changes: 2 additions & 0 deletions tests/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Nullstack from 'nullstack'

import cors from 'cors'
import express from 'express'

import Application from './src/Application'
import CatchError from './src/CatchError'
Expand All @@ -24,6 +25,7 @@ context.server.use(
optionsSuccessStatus: 200,
}),
)
context.server.use(express.json())

context.worker.staleWhileRevalidate = [/[0-9]/]
context.worker.cacheFirst = [/[0-9]/]
Expand Down
37 changes: 25 additions & 12 deletions tests/src/ExposedServerFunctions.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import Nullstack from 'nullstack'

async function api(method) {
const body = {
async function api(method, contentType = 'text/plain') {
const payload = {
number: 69,
date: new Date(),
string: 'nullstack',
}
const response = await fetch(`/data/${method}/param?query=query&truthy=true&falsy=false`, {
method: method.toUpperCase(),
body: method === 'get' ? undefined : JSON.stringify(body),
...(method !== 'get' && {
headers: {
'Content-Type': contentType,
},
body: JSON.stringify(payload),
}),
})
const data = await response.json()
return data.status
Expand Down Expand Up @@ -38,22 +43,30 @@ class ExposedServerFunctions extends Nullstack {
async hydrate() {
this.chainableServerFunction = await chainable('server')
this.chainableRegularFunction = await chainable('regular')
this.all = await api('get')
this.all = await api('get') // TODO we may remove this as it is the pretty much the same as get
this.get = await api('get')
this.post = await api('post')
this.put = await api('put')
this.patch = await api('patch')
this.delete = await api('delete')
this.postTextPayload = await api('post')
this.postJsonPayload = await api('post', 'application/json')
this.putTextPayload = await api('put')
this.putJsonPayload = await api('put', 'application/json')
this.patchTextPayload = await api('patch')
this.patchJsonPayload = await api('patch', 'application/json')
this.deleteTextPayload = await api('delete')
this.deleteJsonPayload = await api('delete', 'application/json')
}

render() {
return (
<div
data-get={this.get}
data-post={this.post}
data-put={this.put}
data-patch={this.patch}
data-delete={this.delete}
data-post={this.postTextPayload}
data-post-json={this.postJsonPayload}
data-put={this.putTextPayload}
data-put-json={this.putJsonPayload}
data-patch={this.patchTextPayload}
data-patch-json={this.patchJsonPayload}
data-delete={this.deleteTextPayload}
data-delete-json={this.deleteJsonPayload}
data-all={this.all}
data-chainable-server-function={this.chainableServerFunction}
data-chainable-regular-function={this.chainableRegularFunction}
Expand Down
32 changes: 28 additions & 4 deletions tests/src/ExposedServerFunctions.test.js