sliftutils 0.8.0 → 0.9.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/.cursorrules +24 -0
- package/misc/getSecret.ts +32 -0
- package/package.json +1 -1
package/.cursorrules
CHANGED
|
@@ -22,9 +22,29 @@ We use MobX for state management. Components should use a variable called synced
|
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
The code automatically updates on save, so do not ever run commands to rerun the site.
|
|
26
|
+
|
|
27
|
+
In the implementation for every type of API, there should be a companion file to manage the session (ex smsSession.ts). This should export a function which makes calling the API functions simple by automatically managing the session. Because the session helper will be making the function calls, it can try catch them and appropriately handle the session errors that require regenerating the session. Of course, if it's not a session error, it should just rethrow it.
|
|
28
|
+
|
|
29
|
+
Use createPersistentObject for persistent things like session states. It's accessed synchronously and the key will have an undefined value if it the value isn't set.
|
|
30
|
+
|
|
31
|
+
let persistent = createPersistentObject<{
|
|
32
|
+
token: string;
|
|
33
|
+
}>("sms");
|
|
34
|
+
|
|
35
|
+
persistent.token = "...";
|
|
36
|
+
persistent.token;
|
|
37
|
+
|
|
38
|
+
|
|
25
39
|
Coding Styles
|
|
26
40
|
Try not to use "null", and instead always use "undefined".
|
|
27
41
|
|
|
42
|
+
Almost never check for undefined or null, just check for falsey.
|
|
43
|
+
|
|
44
|
+
When functions have more than one primitive parameter, such that you could confuse them, even something as simple as start time and end time, you should put them inside of an object and call that object config, so the function only has a single parameter.
|
|
45
|
+
|
|
46
|
+
Never use return codes, always prefer to throw on error. Include a lot of context information in the error. If any values might be extremely large, such as parsing a file, limit them (ex, to 500 characters).
|
|
47
|
+
|
|
28
48
|
He used double quotes, not single quotes.
|
|
29
49
|
|
|
30
50
|
Never use the ternary operator. Instead, do this: "x ? y : z" => "x && y || z".
|
|
@@ -53,6 +73,10 @@ Coding Styles
|
|
|
53
73
|
|
|
54
74
|
Do not use types when they can be inferred.
|
|
55
75
|
|
|
76
|
+
Constants that are arbitrary and that we might to reconfigure should be near the top of the file under the imports, not buried within functions.
|
|
77
|
+
|
|
78
|
+
Never use environment variables. All configuration should be on the disk, or, if specific to a current run, passed as command line parameters.
|
|
79
|
+
|
|
56
80
|
|
|
57
81
|
General Styling
|
|
58
82
|
Never use em or rem. Only use px or vw/vh/%.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
|
|
2
|
+
// Ex, sms.json.id => os.homedir() + "/sms.json", then read key "id"
|
|
3
|
+
// - As in, we expect .json, and use that to determine the path vs key.
|
|
4
|
+
// - If there is no .json, we just assume the entire key is the path (still os.homedir()), and return the entire file.
|
|
5
|
+
|
|
6
|
+
import { cache } from "socket-function/src/caching";
|
|
7
|
+
import os from "os";
|
|
8
|
+
import fs from "fs";
|
|
9
|
+
|
|
10
|
+
export const getSecret = cache(async function getSecret(key: string): Promise<string> {
|
|
11
|
+
const jsonIndex = key.indexOf(".json");
|
|
12
|
+
if (jsonIndex === -1) {
|
|
13
|
+
const filePath = os.homedir() + "/" + key;
|
|
14
|
+
return fs.readFileSync(filePath, "utf-8").trim();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const pathPart = key.slice(0, jsonIndex + ".json".length);
|
|
18
|
+
const filePath = os.homedir() + "/" + pathPart;
|
|
19
|
+
const contents = fs.readFileSync(filePath, "utf-8");
|
|
20
|
+
const json = JSON.parse(contents);
|
|
21
|
+
|
|
22
|
+
const keyPart = key.slice(jsonIndex + ".json.".length);
|
|
23
|
+
if (!keyPart) {
|
|
24
|
+
return JSON.stringify(json);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const value = json[keyPart];
|
|
28
|
+
if (value === undefined) {
|
|
29
|
+
throw new Error(`Expected key "${keyPart}" in ${filePath}, was undefined`);
|
|
30
|
+
}
|
|
31
|
+
return String(value);
|
|
32
|
+
});
|