-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from doseofted/support-rpc-prop-verbs
Support new RPC keyword for idempotency
- Loading branch information
Showing
21 changed files
with
488 additions
and
275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@doseofted/prim-rpc": minor | ||
--- | ||
|
||
.methodsOnMethods option now requires an key/value object where the key is the method-on-method name and the value is either `true` or `"idempotent"` (similar to .allowList option) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@doseofted/prim-rpc": minor | ||
--- | ||
|
||
RPC can no longer be made by GET requests by default: introduced new keyword for function's `.rpc` property named "idempotent" that, when used with HTTP plugins, allows RPC over GET requests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Check that a function can be executed as RPC based on the given `.rpc` property. The `.rpc` property can be retrieved | ||
* from a function by calling `getFunctionRpcProperty()`. | ||
* | ||
* This compares the given `rpcSpecifier` with given request options (currently, only the `httpMethod`). | ||
* | ||
* By default, all requests sent over a network to Prim+RPC are POST-like: | ||
* | ||
* - When `.rpc` is `true`, the HTTP method must be `"POST"` when applicable or `false` (inapplicable, i.e. IPC) | ||
* - When `.rpc` is `"idempotent"` (a special keyword), the HTTP method may also be `"GET"` | ||
* | ||
* @param rpcSpecifier The value of a function's `.rpc` property | ||
* @param httpMethod The optional HTTP method used in a request | ||
* @returns whether function can be called based on given RPC property and given request options | ||
*/ | ||
export function checkRpcIdentifier(rpcSpecifier: string | boolean, httpMethod: string | false) { | ||
const postMethodAllowed = | ||
typeof rpcSpecifier === "boolean" && rpcSpecifier && (httpMethod === "POST" || httpMethod === false) | ||
if (postMethodAllowed) return postMethodAllowed | ||
const getMethodAllowed = | ||
typeof rpcSpecifier === "string" && | ||
rpcSpecifier === "idempotent" && | ||
(typeof httpMethod === "string" ? ["GET", "POST"].includes(httpMethod) : httpMethod === false) | ||
if (getMethodAllowed) return getMethodAllowed | ||
return false | ||
} | ||
|
||
/** | ||
* Given a function, grab the `.rpc` property. **This does not check that the `.rpc` property is valid** only that it | ||
* exists and it is the expected scalar type. | ||
* | ||
* @param possibleFunction A possible function given on a module | ||
*/ | ||
export function getFunctionRpcProperty(possibleFunction?: unknown) { | ||
return ( | ||
typeof possibleFunction === "function" && | ||
"rpc" in possibleFunction && | ||
(typeof possibleFunction.rpc === "string" || typeof possibleFunction.rpc === "boolean") && | ||
possibleFunction.rpc | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.