@foxtware/mineral 0.1.40 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +54 -35
- package/api/shopify/shopifyProductUpdateTrigger.js +16 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,21 +1,24 @@
|
|
|
1
1
|
# mineral
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An everything middleware, centred around easy-to-bring credentials and instantly useful curl commands. Now importable into your project.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## How to get mineralling
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
7
|
+
1. From your project, install mineral: `npm i @foxtware/mineral`.
|
|
8
|
+
2. Set up a `.creds.yml` in your project directory, based on mineral's `.creds.yml.sample`. Add the credentials for the platform you want to use.
|
|
9
|
+
3. Add the `dev` command to your project's `package.json`:
|
|
10
|
+
```
|
|
11
|
+
"dev": "node --watch --watch-path=./api --watch-path=./.creds.yml --watch-path=./.env --watch-path=./hosting node_modules/@foxtware/mineral/server.js --workspace . --api_dirs api"
|
|
12
|
+
```
|
|
13
|
+
4. Run `npm run dev` to start a server.
|
|
14
|
+
5. Use an example curl command from the file whose function you want to use, with the creds path you set up.
|
|
14
15
|
|
|
15
|
-
##
|
|
16
|
+
## Key ideas
|
|
17
|
+
### The response format to rule them all
|
|
16
18
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
+
All functions should return data in this format.
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
19
22
|
interface Response {
|
|
20
23
|
ok: boolean
|
|
21
24
|
data?: unknown
|
|
@@ -32,28 +35,44 @@ The [bedrock](https://github.com/GorgonFreeman/bedrock) middleware, refactored f
|
|
|
32
35
|
}
|
|
33
36
|
results?: Response[]
|
|
34
37
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
38
|
+
```
|
|
39
|
+
- `ok` — `true` if the intent executed; `false` otherwise
|
|
40
|
+
- `data` — any info returned
|
|
41
|
+
- `error` — when present, multiple errors go in `error.details`
|
|
42
|
+
- `meta` — pagination, total items, etc.
|
|
43
|
+
- `results` — array of responses from a queue or batch of calls
|
|
44
|
+
- [Examples](_docs/standard_response_examples.md)
|
|
45
|
+
|
|
46
|
+
### BYO creds
|
|
47
|
+
|
|
48
|
+
Each function accepts an object like this, supplying auth info for each platform it contacts:
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
{
|
|
52
|
+
credsPath,
|
|
53
|
+
credsObject,
|
|
54
|
+
credsProvider, // function that returns a credsObject
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
You can set up a `.creds.yml` in your workspace directory and refer to it using `credsPath` > `platform.account`, or supply credentials inline using `credsObject`.
|
|
59
|
+
|
|
60
|
+
### Monorepo context
|
|
61
|
+
|
|
62
|
+
Mineral gets pushed to from a larger repo. Private functions are contained in a sibling project, which imports mineral to power all of the core functionalities, and layers certain business logic on top - e.g. known creds paths.
|
|
63
|
+
|
|
64
|
+
Mineral is strictly core functionalities, useful for anyone.
|
|
65
|
+
|
|
66
|
+
Pass `--workspace` to locate `.creds.yml` and `--api_dirs` to serve additional function directories.
|
|
67
|
+
|
|
68
|
+
### Hosting
|
|
69
|
+
|
|
70
|
+
For cloud deploy, workspaces use `hosting/.hosting.yml` and `npm run host` (same `--workspace` / `--api_dirs` flags as dev/serve). See `hosting/.hosting.yml.sample`.
|
|
71
|
+
|
|
72
|
+
In the hosting YML, you can use wrappers which are additional layers to your hosted function. For instance, you may want to require an API key in requests to your middleware while it's live, or, require a hash in the headers that matches a hash of the body.
|
|
73
|
+
|
|
74
|
+
The decoupled nature of these desires to what the function actually does and their broad application is why they're implemented in the hosting config itself, rather than in a custom instance of the function.
|
|
53
75
|
|
|
54
|
-
|
|
76
|
+
### AI use
|
|
55
77
|
|
|
56
|
-
|
|
57
|
-
- Server makes functions available from the api/ route, where an export matches the filename. Run `npm run serve`, and they're all curlable.
|
|
58
|
-
- .creds.yml is copied into .env when deploying, so creds can be accessed while hosted. Locally, it reads from the file directly.
|
|
59
|
-
- Cloud deploy reads `hosting/.hosting.yml` for per-function config — `before_wrappers` / `after_wrappers` like `requireHostedApiKey`, `max_instances`, schedules — and deploys each function to Google Cloud.
|
|
78
|
+
Not gonna lie, I have absolutely composed some of these platforms with AI. Some of them may not work, some may not stick closely to the core principles, some may use awkward auth methods - some error payloads are definitely not optimised. However, know that I have hand-coded precursors to this repo, that do largely the same thing, and that any additions benefit (or suffer) from the structure that is laid out in the earlier platforms.
|
|
@@ -6,32 +6,41 @@ const { ArgsWarden } = require('../utils');
|
|
|
6
6
|
const { shopifyProductGet, productIdentifierValidator } = require('../shopify/shopifyProductGet');
|
|
7
7
|
const { shopifyProductUpdate } = require('../shopify/shopifyProductUpdate');
|
|
8
8
|
|
|
9
|
+
const productPayloadValidator = (productPayload) => {
|
|
10
|
+
const { productIdentifier, productTitle } = productPayload;
|
|
11
|
+
return productIdentifierValidator(productIdentifier);
|
|
12
|
+
};
|
|
13
|
+
|
|
9
14
|
const argsWarden = new ArgsWarden([
|
|
10
15
|
['credsPayload', credsValidator],
|
|
11
|
-
['
|
|
16
|
+
['productPayload', productPayloadValidator],
|
|
12
17
|
]);
|
|
13
18
|
|
|
14
19
|
const shopifyProductUpdateTrigger = async (
|
|
15
20
|
credsPayload,
|
|
16
|
-
|
|
21
|
+
productPayload,
|
|
17
22
|
{
|
|
18
23
|
apiVersion,
|
|
19
|
-
productTitle,
|
|
20
24
|
} = {},
|
|
21
25
|
) => {
|
|
22
26
|
|
|
23
27
|
const rejectResponse = await argsWarden.responseIfRejectingArgs({
|
|
24
28
|
credsPayload,
|
|
25
|
-
|
|
29
|
+
productPayload,
|
|
26
30
|
});
|
|
27
31
|
if (rejectResponse) {
|
|
28
32
|
return rejectResponse;
|
|
29
33
|
}
|
|
30
34
|
|
|
35
|
+
const {
|
|
36
|
+
productIdentifier,
|
|
37
|
+
productTitle,
|
|
38
|
+
} = productPayload;
|
|
39
|
+
|
|
31
40
|
if (!productTitle) {
|
|
32
41
|
const productResponse = await shopifyProductGet(
|
|
33
42
|
credsPayload,
|
|
34
|
-
|
|
43
|
+
productPayload,
|
|
35
44
|
{
|
|
36
45
|
attrs: 'title',
|
|
37
46
|
},
|
|
@@ -83,14 +92,13 @@ module.exports = {
|
|
|
83
92
|
-H "Content-Type: application/json" \
|
|
84
93
|
-d '{
|
|
85
94
|
"credsPayload": { "credsPath": "shopify.au" },
|
|
86
|
-
"productIdentifier": { "productId": "1234567890" }
|
|
95
|
+
"productPayload": { "productIdentifier": { "productId": "1234567890" } }
|
|
87
96
|
}'
|
|
88
97
|
|
|
89
98
|
curl -X POST "http://localhost:8000/shopifyProductUpdateTrigger" \
|
|
90
99
|
-H "Content-Type: application/json" \
|
|
91
100
|
-d '{
|
|
92
101
|
"credsPayload": { "credsPath": "shopify.au" },
|
|
93
|
-
"productIdentifier": { "productId": "1234567890" },
|
|
94
|
-
"options": { "productTitle": "Ultra Strength Freeze Ray" }
|
|
102
|
+
"productPayload": { "productIdentifier": { "productId": "1234567890" }, "productTitle": "Ultra Strength Freeze Ray" },
|
|
95
103
|
}'
|
|
96
104
|
*/
|