Skip to content

Commit

Permalink
accept FormData + string as data argument
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanmcgowan committed Nov 10, 2023
1 parent b9ad2fc commit 2b472ef
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/silver-pianos-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"submitjson": minor
---

Initialize the submitjson client
5 changes: 5 additions & 0 deletions .changeset/strong-dragons-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"submitjson": patch
---

accept FormData and string as data argument
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
version: 7
- uses: actions/setup-node@v3
with:
node-version: 18.x
node-version: 20.x
cache: pnpm

- run: pnpm install --frozen-lockfile
Expand Down
31 changes: 22 additions & 9 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,28 @@ export default class SubmitJSON {
}
}

async submit(data: Record<string, unknown>, options?: SubmitOptions | string, endpoint?: string) {
async submit(data: Record<string, unknown> | FormData | string, options?: SubmitOptions | string, endpoint?: string) {
try {
// validate that data is an object
if (typeof data !== 'object')
throw new Error('param "data" must be a valid JSON object')

// make sure the object is valid JSON
JSON.stringify(data)

// **HANDLE DATA**
let d: Record<string, unknown>
if (data instanceof FormData) {
// validate form data first, ts error otherwise
JSON.stringify(Object.fromEntries(data))
d = Object.fromEntries(data)
}
else if (typeof data === 'string') {
// validate that string is valid json next
d = JSON.parse(data)
}
else if (typeof data === 'object') {
// finally make sure the object is valid
JSON.stringify(data)
d = data
}
else {
throw new TypeError('The first argument must be a valid JSON object, string, or FormData')
}
// **HANDLE OPTIONS**
// if second param is a string assume it is an endpoint
if (typeof options === 'string')
endpoint = options
Expand All @@ -58,7 +71,7 @@ export default class SubmitJSON {
throw new Error('👻 No endpoint defined. Add one to your client configuration or to this submit call.')

// define the body to submit in a sec
const body: RequestBody = { data }
const body: RequestBody = { data: d }

// define an empty options variable
let o: SubmitOptions | undefined
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"version": "0.0.1",
"description": "REST API client for submitjson.com",
"author": "Dylan McGowan",
"license": "UNLICENSED",
"license": "MIT",
"homepage": "https://www.submitjson.com",
"repository": "https://github.com/dylanmcgowan/submitjson",
"keywords": [],
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down

0 comments on commit 2b472ef

Please sign in to comment.