google / zx

A tool for writing better scripts

Home Page:https://npmjs.com/package/zx

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FormData from node-fetch made available as a global

javascriptjedi opened this issue · comments

Proposal

zx doesn't seem to have a way to use FormData with a fetch request. node-fetch provides a polyfill already. Would it be possible to also make FormData a global?

Actual Behavior

Can't use FormData easily in zx CI scripts.

Please, share an example.

I'm working in GitLab and their api supports multipart/form-data requests.

Two quick usage examples of this content type in a request from their docs:

https://docs.gitlab.com/ee/development/documentation/restful_api_styleguide.html#post-data-using-form-data
https://docs.gitlab.com/ee/api/wikis.html#upload-an-attachment-to-the-wiki-repository

From node-fetch docs:

node-fetch comes with a spec-compliant FormData implementations for posting multipart/form-data payloads

import fetch, { FormData, File, fileFrom } from 'node-fetch'

const httpbin = 'https://httpbin.org/post'
const formData = new FormData()
const binary = new Uint8Array([ 97, 98, 99 ])
const abc = new File([binary], 'abc.txt'), { type: 'text/plain' })

formData.set('greeting', 'Hello, world!')
formData.set('file-upload', abc, 'new name.txt')

const response = await fetch(httpbin, { method: 'POST', body: formData })
const data = await response.json()

All I was saying is if zx is already using node-fetch under the hood, could node-fetch's FormData also be made available for this and other potential use cases of zx in CI/CD environments.

You can use

import { FormData, File, fileFrom } from 'node-fetch'

Sounds good and sounds like it is just a problem with the Docker image configuration we have that brings in zx then. I tried that already and node-fetch wasn't available in my script, it couldn't be found. I'll try to fix the pathing/resolution. Thanks for the response.

ezoic increase your site revenue