Skip to content

Commit

Permalink
Adds handling of server actions where argument is array of objects (#64)
Browse files Browse the repository at this point in the history
* handles server actions where argument is array of objects
* fixed linting issue
* upgraded github workflow action pnpm to 9
  • Loading branch information
amorey authored Sep 22, 2024
1 parent b6a5c59 commit 12f57f4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
node-version: 20
- uses: pnpm/action-setup@v3
with:
version: 8
version: 9
- run: pnpm install
- run: pnpm -r lint

Expand All @@ -28,7 +28,7 @@ jobs:
node-version: 20
- uses: pnpm/action-setup@v3
with:
version: 8
version: 9
- run: pnpm install
- run: pnpm -r test run -- --environment node
- run: pnpm -r test run -- --environment edge-runtime
Expand All @@ -44,6 +44,6 @@ jobs:
node-version: 20
- uses: pnpm/action-setup@v3
with:
version: 8
version: 9
- run: pnpm install
- run: pnpm -r build
18 changes: 17 additions & 1 deletion shared/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,25 @@ export async function getTokenString(request: Request, valueFn?: TokenValueFunct
// non-form server actions
if (contentType.startsWith('text/plain')) {
try {
// handle array of arguments
const args = JSON.parse(rawVal);

if (!Array.isArray(args) || args.length === 0) return rawVal;
return args[0];

const args0 = args[0];
const typeofArgs0 = typeof args0;

if (typeofArgs0 === 'string') {
// treat first string argument as csrf token
return args0;
}

if (typeofArgs0 === 'object') {
// if first argument is an object, look for token there
return args0.csrf_token || '';
}

return args0;
} catch (e) {
return rawVal;
}
Expand Down

0 comments on commit 12f57f4

Please sign in to comment.