@@ -5,8 +5,6 @@ import FormData from "form-data";
55import axios from "axios" ;
66import { readdirSync , readFileSync } from "fs-extra" ;
77
8- const isValidUri = ( uri : Uri ) => uri . authority === "fs" ;
9-
108const dirToFormData = ( baseDirPath : string ) => {
119 const formData = new FormData ( ) ;
1210
@@ -34,28 +32,43 @@ const dirToFormData = (baseDirPath: string) => {
3432} ;
3533
3634class HTTPDeployer implements DeployModule {
37- async execute ( uri : Uri , config ?: { postUrl : string } ) : Promise < Uri > {
38- if ( ! isValidUri ( uri ) ) {
39- throw new Error (
40- `HTTP Deployer error: Invalid URI: ${ uri . toString ( ) } . Supplied URI needs to be a Filesystem URI, example: fs/./build`
41- ) ;
42- }
43-
35+ async execute (
36+ uri : Uri ,
37+ config ?: { postUrl : string ; headers : { name : string ; value : string } [ ] }
38+ ) : Promise < Uri > {
4439 if ( ! config ?. postUrl ) {
4540 throw new Error (
4641 `HTTP Deployer error: No postUrl provided. Please provide a postUrl in the deploy manifest's config object`
4742 ) ;
4843 }
4944
50- const formData = dirToFormData ( uri . path ) ;
45+ let response ;
46+ if ( uri . authority === "fs" || uri . authority === "file" ) {
47+ // URI is a FileSystem URI, so we read the directory and publish it
48+ const formData = dirToFormData ( uri . path ) ;
5149
52- const response = await axios . post < { uri : string ; error : string } > (
53- config . postUrl ,
54- formData ,
55- {
56- headers : formData . getHeaders ( ) ,
57- }
58- ) ;
50+ response = await axios . post < { uri : string ; error : string } > (
51+ config . postUrl ,
52+ formData ,
53+ {
54+ headers : {
55+ ...formData . getHeaders ( ) ,
56+ ...parseConfigHeaders ( config . headers ) ,
57+ } ,
58+ }
59+ ) ;
60+ } else {
61+ // URI is of another kind, so we just publish it as a redirect
62+ response = await axios . post < { uri : string ; error : string } > (
63+ config . postUrl ,
64+ {
65+ uri : uri . toString ( ) ,
66+ } ,
67+ {
68+ headers : parseConfigHeaders ( config . headers ) ,
69+ }
70+ ) ;
71+ }
5972
6073 if ( response . data . error ) {
6174 throw new Error ( response . data . error ) ;
@@ -65,4 +78,14 @@ class HTTPDeployer implements DeployModule {
6578 }
6679}
6780
81+ function parseConfigHeaders ( configHeaders : { name : string ; value : string } [ ] ) {
82+ const headers : { [ key : string ] : string } = { } ;
83+
84+ configHeaders . forEach ( ( header ) => {
85+ headers [ header . name ] = header . value ;
86+ } ) ;
87+
88+ return headers ;
89+ }
90+
6891export default new HTTPDeployer ( ) ;
0 commit comments