@cloud-cli/env 1.1.4 → 1.3.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/dist/index.d.ts CHANGED
@@ -1,12 +1,15 @@
1
- import { Resource } from '@cloud-cli/store';
2
- import { init } from '@cloud-cli/cli';
3
- declare class EnvEntry extends Resource {
1
+ export declare class EnvEntry {
4
2
  app: string;
5
3
  key: string;
6
4
  value: string;
7
5
  }
8
6
  export interface App {
9
7
  app: string;
8
+ name?: string;
9
+ }
10
+ export interface ListFilters {
11
+ app?: string;
12
+ key?: string;
10
13
  }
11
14
  export interface KeyValue {
12
15
  key: string;
@@ -14,19 +17,22 @@ export interface KeyValue {
14
17
  }
15
18
  export interface AppKeyValue extends App, KeyValue {
16
19
  }
17
- declare function reload(): Promise<void>;
18
20
  declare function show(options: App): Promise<EnvEntry[]>;
21
+ declare function list(filters?: ListFilters): Promise<EnvEntry[]>;
19
22
  declare function apps(): Promise<string[]>;
20
- declare function set(options: AppKeyValue): Promise<EnvEntry>;
21
- declare function remove(options: AppKeyValue): Promise<any>;
22
- declare function get(options: Omit<AppKeyValue, 'value'>): Promise<EnvEntry[]>;
23
+ declare function setVar(options: AppKeyValue): Promise<{
24
+ app: string;
25
+ key: string;
26
+ value: string;
27
+ }>;
28
+ declare function removeVar(options: AppKeyValue): Promise<boolean>;
29
+ declare function getVar(options: Omit<AppKeyValue, 'value'>): Promise<EnvEntry>;
23
30
  declare const _default: {
24
- get: typeof get;
25
- set: typeof set;
31
+ get: typeof getVar;
32
+ set: typeof setVar;
33
+ remove: typeof removeVar;
26
34
  show: typeof show;
27
- remove: typeof remove;
28
35
  apps: typeof apps;
29
- reload: typeof reload;
30
- [init]: typeof reload;
36
+ list: typeof list;
31
37
  };
32
38
  export default _default;
package/dist/index.js CHANGED
@@ -1,73 +1,74 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- import { Model, NotNull, Property, Query, Resource, SQLiteDriver, Unique } from '@cloud-cli/store';
8
- import { init } from '@cloud-cli/cli';
1
+ import { getStorage } from '@cloud-cli/cli';
9
2
  const appNotSpecifiedError = new Error('App not specified');
10
3
  const keyNotSpecifiedError = new Error('Key not specified');
11
- let EnvEntry = class EnvEntry extends Resource {
12
- };
13
- __decorate([
14
- Unique(),
15
- NotNull(),
16
- Property(String)
17
- ], EnvEntry.prototype, "app", void 0);
18
- __decorate([
19
- Unique(),
20
- NotNull(),
21
- Property(String)
22
- ], EnvEntry.prototype, "key", void 0);
23
- __decorate([
24
- Property(String)
25
- ], EnvEntry.prototype, "value", void 0);
26
- EnvEntry = __decorate([
27
- Model('env')
28
- ], EnvEntry);
29
- async function reload() {
30
- Resource.use(new SQLiteDriver());
31
- await Resource.create(EnvEntry);
4
+ const { get, set, remove, getAll } = getStorage('env');
5
+ export class EnvEntry {
32
6
  }
33
7
  async function show(options) {
34
- const { app } = options;
8
+ const app = options.app || options.name;
35
9
  if (!app) {
36
10
  throw appNotSpecifiedError;
37
11
  }
38
- return Resource.find(EnvEntry, new Query().where('app').is(app));
12
+ return list({ app });
13
+ }
14
+ async function list(filters = {}) {
15
+ let all = await getAll();
16
+ if (filters.app) {
17
+ all = all.filter((e) => e.app === filters.app);
18
+ }
19
+ if (filters.key) {
20
+ all = all.filter((e) => e.key === filters.key);
21
+ }
22
+ return all;
39
23
  }
40
24
  async function apps() {
41
- const rows = await Resource.find(EnvEntry, new Query());
42
- return Array.from(new Set(rows.map(entry => entry.app)));
25
+ const rows = await getAll();
26
+ const apps = rows.map((entry) => entry.app);
27
+ return [...new Set(apps)];
43
28
  }
44
- async function set(options) {
45
- const { app, key, value } = options;
29
+ async function setVar(options) {
30
+ const app = options.app || options.name;
31
+ const { key, value } = options;
46
32
  if (!app) {
47
33
  throw appNotSpecifiedError;
48
34
  }
49
35
  if (!key) {
50
36
  throw keyNotSpecifiedError;
51
37
  }
52
- const entry = new EnvEntry({ app, key, value });
53
- await entry.save();
38
+ const entry = { app, key, value };
39
+ await set(computeId(app, key), entry);
54
40
  return entry;
55
41
  }
56
- async function remove(options) {
57
- const { app, key } = options;
58
- const found = await get({ app, key });
59
- if ((found).length) {
60
- return void await found[0].remove();
42
+ async function removeVar(options) {
43
+ const app = options.app || options.name;
44
+ const { key } = options;
45
+ const found = await getVar({ app, key });
46
+ if (found) {
47
+ await remove(computeId(app, key));
48
+ return true;
61
49
  }
50
+ return false;
62
51
  }
63
- async function get(options) {
64
- const { app, key } = options;
52
+ async function getVar(options) {
53
+ const app = options.app || options.name;
54
+ const { key } = options;
65
55
  if (!app) {
66
56
  throw appNotSpecifiedError;
67
57
  }
68
58
  if (!key) {
69
59
  throw keyNotSpecifiedError;
70
60
  }
71
- return Resource.find(EnvEntry, new Query().where('app').is(app).where('key').is(key));
61
+ return get(computeId(app, key));
62
+ }
63
+ const invalidRe = /[^a-z09-]/g;
64
+ function computeId(app, key) {
65
+ return `${app}.${key}`.replace(invalidRe, '-');
72
66
  }
73
- export default { get, set, show, remove, apps, reload, [init]: reload };
67
+ export default {
68
+ get: getVar,
69
+ set: setVar,
70
+ remove: removeVar,
71
+ show,
72
+ apps,
73
+ list,
74
+ };
package/package.json CHANGED
@@ -1,14 +1,13 @@
1
1
  {
2
2
  "name": "@cloud-cli/env",
3
- "version": "1.1.4",
3
+ "version": "1.3.0",
4
4
  "description": "Storage for app environment variables",
5
- "main": "dist/index.js",
6
5
  "type": "module",
6
+ "main": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
8
8
  "prettier": "@cloud-cli/prettier-config",
9
9
  "scripts": {
10
- "test": "jest",
11
- "tdd": "jest --watchAll",
10
+ "test": "vitest",
12
11
  "build": "rm -rf ./dist/* && tsc --project ./tsconfig.json"
13
12
  },
14
13
  "keywords": [
@@ -16,9 +15,11 @@
16
15
  ],
17
16
  "license": "MIT",
18
17
  "devDependencies": {
19
- "@cloud-cli/jest-config": "^1.0.0",
20
18
  "@cloud-cli/prettier-config": "^1.0.0",
21
- "@cloud-cli/typescript-config": "^1.0.1"
19
+ "@cloud-cli/typescript-config": "^1.0.1",
20
+ "@types/node": "^20.12.8",
21
+ "typescript": "^5.4.5",
22
+ "vitest": "^1.6.0"
22
23
  },
23
24
  "repository": {
24
25
  "type": "git",
@@ -28,7 +29,6 @@
28
29
  "access": "public"
29
30
  },
30
31
  "dependencies": {
31
- "@cloud-cli/cli": "^1.4.4",
32
- "@cloud-cli/store": "^1.5.1"
32
+ "@cloud-cli/cli": "^1.4.4"
33
33
  }
34
34
  }