@@ -62,6 +62,7 @@ console.log(response.body.toString())
62
62
* ` docroot ` {String} Document root for PHP. ** Default:** process.cwd()
63
63
* ` throwRequestErrors ` {Boolean} Throw request errors rather than returning
64
64
responses with error codes. ** Default:** false
65
+ * ` rewriter ` {Rewriter} Optional rewrite rules. ** Default:** ` undefined `
65
66
* Returns: {Php}
66
67
67
68
Construct a new PHP instance to which to dispatch requests.
@@ -560,9 +561,111 @@ headers.forEach((value, name, headers) => {
560
561
})
561
562
```
562
563
564
+ ### ` new Rewriter(input) `
565
+
566
+ * ` rules ` {Array} The set of rewriting rules to apply to each request
567
+ * ` operation ` {String} Operation type (` and ` or ` or ` ) ** Default:** ` and `
568
+ * ` conditions ` {Array} Conditions to match against the request
569
+ * ` type ` {String} Condition type
570
+ * ` args ` {String} Arguments to pass to condition constructor
571
+ * ` rewriters ` {Array} Rewrites to apply if the conditions match
572
+ * ` type ` {String} Rewriter type
573
+ * ` args ` {String} Arguments to pass to rewriter constructor
574
+ * Returns: {Rewriter}
575
+
576
+ Construct a Rewriter to rewrite requests before they are dispatched to PHP.
577
+
578
+ ``` js
579
+ import { Rewriter } from ' @platformatic/php-node'
580
+
581
+ const rewriter = new Rewriter ([{
582
+ conditions: [{
583
+ type: ' header' ,
584
+ args: [' User-Agent' , ' ^(Mozilla|Chrome)' ]
585
+ }],
586
+ rewriters: [{
587
+ type: ' path' ,
588
+ args: [' ^/old-path/(.*)$' , ' /new-path/$1' ]
589
+ }]
590
+ }])
591
+ ```
592
+
593
+ #### Conditions
594
+
595
+ There are several types of conditions which may be used to match against the
596
+ request. Each condition type has a set of arguments which are passed to the
597
+ constructor of the condition. The condition will be evaluated against the
598
+ request and if it matches, the rewriters will be applied.
599
+
600
+ The available condition types are:
601
+
602
+ - ` exists ` Matches if request path exists in docroot.
603
+ - ` not_exists ` Matches if request path does not exist in docroot.
604
+ - ` header(name, pattern) ` Matches named header against a pattern.
605
+ - ` name ` {String} The name of the header to match.
606
+ - ` pattern ` {String} The regex pattern to match against the header value.
607
+ - ` method(pattern) ` Matches request method against a pattern.
608
+ - ` pattern ` {String} The regex pattern to match against the HTTP method.
609
+ - ` path(pattern) ` : Matches request path against a pattern.
610
+ - ` pattern ` {String} The regex pattern to match against the request path.
611
+
612
+ #### Rewriters
613
+
614
+ There are several types of rewriters which may be used to rewrite the request
615
+ before it is dispatched to PHP. Each rewriter type has a set of arguments which
616
+ are passed to the constructor of the rewriter. The rewriter will be applied to
617
+ the request if the conditions match.
618
+
619
+ The available rewriter types are:
620
+
621
+ - ` header(name, replacement) ` Sets a named header to a given replacement.
622
+ - ` name ` {String} The name of the header to set.
623
+ - ` replacement ` {String} The replacement string to use for named header.
624
+ - ` href(pattern, replacement) ` Rewrites request path, query, and fragment to
625
+ given replacement.
626
+ - ` pattern ` {String} The regex pattern to match against the request path.
627
+ - ` replacement ` {String} The replacement string to use for request path.
628
+ - ` method(replacement) ` Sets the request method to a given replacement.
629
+ - ` replacement ` {String} The replacement string to use for request method.
630
+ - ` path(pattern, replacement) ` Rewrites request path to given replacement.
631
+ - ` pattern ` {String} The regex pattern to match against the request path.
632
+ - ` replacement ` {String} The replacement string to use for request path.
633
+
634
+ ### ` rewriter.rewrite(request, docroot) `
635
+
636
+ - ` request ` {Object} The request object.
637
+ - ` docroot ` {String} The document root.
638
+
639
+ Rewrites the given request using the rules provided to the rewriter.
640
+
641
+ This is mainly exposed for testing purposes. It is not recommended to use
642
+ directly. Rather, the ` rewriter ` should be provided to the ` Php ` constructor
643
+ to allow rewriting to occur within the PHP environment where it will be aware
644
+ of the original ` REQUEST_URI ` state.
645
+
646
+ ``` js
647
+ import { Rewriter } from ' @platformatic/php-node'
648
+
649
+ const rewriter = new Rewriter ([{
650
+ rewriters: [{
651
+ type: ' path' ,
652
+ args: [' ^(.*)$' , ' /base/$1'
653
+ }]
654
+ }])
655
+
656
+ const request = new Request ({
657
+ url: ' http://example.com/foo/bar'
658
+ })
659
+
660
+ const modified = rewriter .rewrite (request, import .meta.dirname)
661
+
662
+ console .log (modified .url ) // http://example.com/base/foo/bar
663
+ ` ` `
664
+
563
665
## Contributing
564
666
565
- This project is part of the [ Platformatic] ( https://github.com/platformatic ) ecosystem. Please refer to the main repository for contribution guidelines.
667
+ This project is part of the [Platformatic](https://github.com/platformatic)
668
+ ecosystem. Please refer to the main repository for contribution guidelines.
566
669
567
670
## License
568
671
0 commit comments