@mcpher/gas-fakes 1.0.7 → 1.0.8

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.
@@ -8,7 +8,7 @@ import { mergeParamStrings } from '../src/support/utils.js';
8
8
  // all the fake services are here
9
9
  //import '@mcpher/gas-fakes/main.js'
10
10
 
11
- import '../main.js'
11
+ import '../mainnpm.js'
12
12
  import { LRUCache } from 'google-auth-library/build/src/util.js';
13
13
 
14
14
  const testFakes = () => {
package/main.js CHANGED
@@ -1 +1,2 @@
1
- import './src/index.js'
1
+ // testing locally
2
+ import './src/index.js'
package/package.json CHANGED
@@ -21,20 +21,21 @@
21
21
  },
22
22
  "type": "module",
23
23
  "scripts": {
24
- "test": "node --env-file=.env ./test/test.js",
25
- "testdrive": "node --env-file=.env ./test/testdrive.js execute",
26
- "testsheets": "node --env-file=.env ./test/testsheets.js execute",
27
- "testfetch": "node --env-file=.env ./test/testfetch.js execute",
28
- "testsession": "node --env-file=.env ./test/testsession.js execute",
29
- "testutilities": "node --env-file=.env ./test/testutilities.js execute",
30
- "teststores": "node --env-file=.env ./test/teststores.js execute",
31
- "testscriptapp": "node --env-file=.env ./test/testscriptapp.js execute",
32
- "pub": "npm publish --access public"
24
+ "test": "cp mainlocal.js main.js && node --env-file=.env ./test/test.js",
25
+ "testdrive": "cp mainlocal.js main.js && node --env-file=.env ./test/testdrive.js execute",
26
+ "testsheets": "cp mainlocal.js main.js && node --env-file=.env ./test/testsheets.js execute",
27
+ "testfetch": "cp mainlocal.js main.js && node --env-file=.env ./test/testfetch.js execute",
28
+ "testsession": "cp mainlocal.js main.js && node --env-file=.env ./test/testsession.js execute",
29
+ "testutilities": "cp mainlocal.js main.js && node --env-file=.env ./test/testutilities.js execute",
30
+ "teststores": "cp mainlocal.js main.js && node --env-file=.env ./test/teststores.js execute",
31
+ "testscriptapp": "cp mainlocal.js main.js && node --env-file=.env ./test/testscriptapp.js execute",
32
+ "testfiddler": "cp mainlocal.js main.js && node --env-file=.env ./test/testfiddler.js execute",
33
+ "pub": "cp mainlocal.js main.js && npm publish --access public"
33
34
  },
34
35
  "name": "@mcpher/gas-fakes",
35
- "version": "1.0.7",
36
+ "version": "1.0.8",
36
37
  "main": "main.js",
37
38
  "description": "A proof of concept implementation of Apps Script Environment on Node",
38
39
  "repository": "github:brucemcpherson/gas-fakes",
39
40
  "homepage": "https://ramblings.mcpher.com/a-proof-of-concept-implementation-of-apps-script-environment-on-node/"
40
- }
41
+ }
@@ -5,8 +5,10 @@
5
5
 
6
6
  import { Proxies } from '../../support/proxies.js'
7
7
  import { Syncit } from '../../support/syncit.js'
8
- import { notYetImplemented, isGood } from '../../support/helpers.js'
8
+ import { notYetImplemented, ssError } from '../../support/helpers.js'
9
9
  import { newFakeSheetValues } from './fakeadvvalues.js'
10
+ import { getWorkbookEntry, setWorkbookEntry, clearWorkbookCache } from "../../support/sheetscache.js"
11
+
10
12
 
11
13
 
12
14
  /**
@@ -18,8 +20,6 @@ class FakeAdvSheetsSpreadsheets {
18
20
 
19
21
  const props = [
20
22
  'getByDataFilter',
21
- 'batchUpdate',
22
- 'create',
23
23
  'DeveloperMetadata',
24
24
  'Sheets']
25
25
 
@@ -35,45 +35,96 @@ class FakeAdvSheetsSpreadsheets {
35
35
  return this.__sheets.toString()
36
36
  }
37
37
 
38
- get Values () {
38
+ get Values() {
39
39
  return newFakeSheetValues(this.__sheets)
40
40
  }
41
41
 
42
+ /**
43
+ * batchUpdate: https://developers.google.com/workspace/sheets/api/reference/rest/v4/spreadsheets/batchUpdate
44
+ * note that the order of args -- requests first then id - is different to usual
45
+ * Batch request looks like this https://developers.google.com/workspace/sheets/api/reference/rest/v4/spreadsheets/request#request
46
+ * @param {object} request batch request {request:Request[]}
47
+ */
48
+ batchUpdate(requests, spreadsheetId, { ss = false } = {}) {
49
+
50
+ // note that in GAS adv sheet service doesnt take the requestBody parameter - it just sends requests as the arg
51
+ // so we need to wrap that in requestbody for the Node API
52
+ const requestBody = requests
53
+
54
+ const pack = {
55
+ prop: "spreadsheets",
56
+ method: "batchUpdate",
57
+ params: {
58
+ spreadsheetId,
59
+ requestBody
60
+ }
61
+ }
62
+
63
+ const { response, data } = Syncit.fxSheets(pack)
64
+
65
+ // naive cache - was an update so zap everything
66
+ clearWorkbookCache(spreadsheetId)
67
+
68
+ // maybe we need to throw an error
69
+ ssError (response, pack.method, ss)
70
+
71
+ return data
72
+ }
73
+
42
74
  /**
43
75
  * get a spreadsheet
44
76
  * @param {string} id
45
77
  * @param {object} options
46
78
  */
47
- get(id, options, { ss = false } ={}) {
79
+ get(id, options, { ss = false } = {}) {
48
80
 
49
- const { response, data } = Syncit.fxSheets({
81
+ const pack = {
50
82
  prop: "spreadsheets",
51
83
  method: "get",
52
84
  params: {
53
85
  spreadsheetId: id,
54
86
  ...options
55
87
  }
56
- })
88
+ }
89
+ const cache = getWorkbookEntry(id, pack)
90
+ if (cache) {
91
+ return cache
92
+ }
93
+
94
+ const { response, data } = Syncit.fxSheets(pack)
57
95
 
58
96
  // maybe we need to throw an error
59
- if (!isGood(response)) {
60
- // driveapp throws this error with a bad id
61
- if (ss) {
62
- throw new Error("Unexpected error while getting the method or property openById on object SpreadsheetApp.")
63
- } else {
64
- // adv drive throws this one
65
- throw new Error("GoogleJsonResponseException: API call to sheets.spreadsheets.get failed with error: Requested entity was not found.")
66
- }
67
- }
97
+ ssError (response, pack.method, ss)
68
98
 
69
- // all is good
99
+ // all is good so write to cache
100
+ setWorkbookEntry(id, pack, data)
70
101
  return data
71
-
72
102
  }
73
103
 
104
+ /**
105
+ * @param {Spreadsheet} resource #https://developers.google.com/workspace/sheets/api/reference/rest/v4/spreadsheets#SpreadsheetProperties
106
+ * @return {Spreadsheet} resource
107
+ */
108
+ create(resource, {ss=false} = {}) {
74
109
 
110
+ const pack = {
111
+ prop: "spreadsheets",
112
+ method: "create",
113
+ params: {
114
+ requestBody: resource
115
+ }
116
+ }
117
+
118
+ // create the sheet
119
+ const { response, data } = Syncit.fxSheets(pack)
120
+
121
+ // maybe we need to throw an error
122
+ ssError (response, pack.method, ss)
75
123
 
124
+ return data
125
+ }
76
126
  }
77
127
 
128
+
78
129
  export const newFakeAdvSheetsSpreadsheets = (...args) => Proxies.guard(new FakeAdvSheetsSpreadsheets(...args))
79
130
 
@@ -1,6 +1,7 @@
1
1
  import { Proxies } from '../../support/proxies.js'
2
- import { notYetImplemented, isGood, throwResponse } from '../../support/helpers.js'
2
+ import { notYetImplemented, ssError } from '../../support/helpers.js'
3
3
  import { Syncit } from '../../support/syncit.js'
4
+ import { getWorkbookEntry, setWorkbookEntry, clearWorkbookCache } from "../../support/sheetscache.js"
4
5
  /**
5
6
  * @file
6
7
  * @imports ../typedefs.js
@@ -35,7 +36,6 @@ class FakeSheetValues {
35
36
  'batchClearByDataFilter',
36
37
  'batchUpdateByDataFilter',
37
38
  'batchClear',
38
- 'batchUpdate',
39
39
  'batchGetByDataFilter',
40
40
  'clear',
41
41
  'update',
@@ -55,21 +55,61 @@ class FakeSheetValues {
55
55
 
56
56
  /**
57
57
  * @param {string} spreadsheetId the spreadsheet id
58
- * @param {string} range the a1 style range including the name of the sheet
58
+ * @param {string} ranges the a1 style range including the name of the sheet
59
59
  * @param {object} options
60
60
  */
61
- get(spreadsheetId, range, options) {
62
- const { data } = Syncit.fxSheets({
61
+ get(spreadsheetId, range, options ={}) {
62
+
63
+ const pack = {
63
64
  subProp: "values",
64
65
  prop: "spreadsheets",
65
66
  method: "get",
66
67
  params: {
67
68
  spreadsheetId: spreadsheetId,
68
- range: range,
69
+ range,
69
70
  ...options
70
71
  }
71
- })
72
- // should be values, majorDimension, range
72
+ }
73
+ const cache = getWorkbookEntry(spreadsheetId, pack)
74
+ if (cache) {
75
+ return cache
76
+ }
77
+
78
+ const { data } = Syncit.fxSheets(pack)
79
+
80
+ setWorkbookEntry(spreadsheetId, pack, data)
81
+ return data
82
+ }
83
+
84
+
85
+ /**
86
+ * batchUpdate: https://developers.google.com/workspace/sheets/api/reference/rest/v4/spreadsheets.values/batchUpdate
87
+ * note that the order of args -- requests first then id - is different to usual
88
+ * Batch request looks like this https://developers.google.com/workspace/sheets/api/reference/rest/v4/spreadsheets/request#request
89
+ * @param {object} request batch request {request:Request[]}
90
+ */
91
+ batchUpdate(requests, spreadsheetId, { ss = false } = {}) {
92
+
93
+ const requestBody = requests
94
+
95
+ const pack = {
96
+ subProp: "values",
97
+ prop: "spreadsheets",
98
+ method: "batchUpdate",
99
+ params: {
100
+ spreadsheetId,
101
+ requestBody
102
+ }
103
+ }
104
+
105
+ const { response, data } = Syncit.fxSheets(pack)
106
+
107
+ // naive cache - was an update so zap everything
108
+ clearWorkbookCache(spreadsheetId)
109
+
110
+ // maybe we need to throw an error
111
+ ssError (response, pack.method, ss)
112
+
73
113
  return data
74
114
  }
75
115
 
@@ -0,0 +1,42 @@
1
+ import { Proxies } from '../../support/proxies.js'
2
+ import { FakeColorBase } from './fakecolorbase.js'
3
+ import { newFakeRgbColor } from './fakergbcolor.js'
4
+ import { newFakeThemeColor } from './fakethemecolor.js'
5
+
6
+ /**
7
+ * @file
8
+ * @imports ../typedefs.js
9
+ */
10
+
11
+ /**
12
+ * create a new FakeColor instance
13
+ * @param {...any} args
14
+ * @returns {FakeColor}
15
+ */
16
+ export const newFakeColor = (...args) => {
17
+ return Proxies.guard(new FakeColor(...args))
18
+ }
19
+
20
+
21
+ class FakeColor extends FakeColorBase {
22
+ constructor(builder) {
23
+ super()
24
+ this.__type = builder.__type
25
+ this.__color = builder.__color
26
+ this.__themeColorType = builder.__themeColorType
27
+ }
28
+ asRgbColor() {
29
+ this.__checkType('RGB', 'RgbColor')
30
+ return newFakeRgbColor(this.__color)
31
+ }
32
+ asThemeColor() {
33
+ this.__checkType('THEME', 'ThemeColor')
34
+ return newFakeThemeColor(this.__themeColorType)
35
+ }
36
+ toString() {
37
+ return 'Color'
38
+ }
39
+
40
+
41
+ }
42
+
@@ -0,0 +1,28 @@
1
+ import { newNummery } from '../../support/nummery.js'
2
+
3
+ /**
4
+ * @file
5
+ * @imports ../typedefs.js
6
+ */
7
+
8
+ export class FakeColorBase {
9
+ // this is color type UNSUPPORTED until built
10
+ constructor() {
11
+ this.__type = 'UNSUPPORTED'
12
+ this.__themeColorType = 'UNSUPPORTED'
13
+ this.__color = null
14
+ }
15
+ __checkType(type, mess) {
16
+ if (type !== this.__type) throw new Error(`Object is not of type ${mess}.`)
17
+ }
18
+
19
+ getColorType() {
20
+ return newNummery(this.__type)
21
+ }
22
+
23
+ getThemeColorType() {
24
+ return newNummery(this.__themeColorType)
25
+ }
26
+
27
+ }
28
+
@@ -0,0 +1,68 @@
1
+ import { Proxies } from '../../support/proxies.js'
2
+ import { newFakeColor } from './fakecolor.js'
3
+ import { newFakeRgbColor } from './fakergbcolor.js'
4
+ import { FakeColorBase } from './fakecolorbase.js'
5
+ import { newFakeThemeColor } from './fakethemecolor.js'
6
+ /**
7
+ * @file
8
+ * @imports ../typedefs.js
9
+ */
10
+ /**
11
+ * create a new FakeColorBuilder instance
12
+ * @param {...any} args
13
+ * @returns {FakeColorBuilder}
14
+ */
15
+ export const newFakeColorBuilder = (...args) => {
16
+ return Proxies.guard(new FakeColorBuilder(...args))
17
+ }
18
+
19
+
20
+ class FakeColorBuilder extends FakeColorBase {
21
+ constructor() {
22
+ super()
23
+ }
24
+
25
+ asRgbColor() {
26
+ this.__checkType('RGB', 'RgbColor')
27
+ return newFakeRgbColor(this.__color)
28
+ }
29
+
30
+ asThemeColor() {
31
+ this.__checkType('THEME', 'ThemeColor')
32
+ return newFakeThemeColor (this.__themeColorType)
33
+ }
34
+
35
+ /**
36
+ * setRgbColor(cssString) https://developers.google.com/apps-script/reference/spreadsheet/color-builder#setrgbcolorcssstring
37
+ * Sets as RGB color
38
+ * @param {string} cssString The RGB color in CSS notation (such as '#ffffff').
39
+ * @returns {FakeColorBuilder} self
40
+ */
41
+ setRgbColor(cssString) {
42
+ this.__color = cssString
43
+ this.__type = 'RGB'
44
+ return this
45
+ }
46
+
47
+ /**
48
+ * setThemeColor(themeColorType) https://developers.google.com/apps-script/reference/spreadsheet/color-builder#setthemecolorthemecolortype
49
+ * Sets as theme color.
50
+ * @param {ThemeColorType} The theme color type.
51
+ * @returns {FakeColorBuilder} self
52
+ */
53
+ setThemeColor(themeColorType) {
54
+ this.__themeColorType = themeColorType
55
+ this.__type = 'THEME'
56
+ return this
57
+ }
58
+
59
+ build() {
60
+ return newFakeColor(this)
61
+ }
62
+ toString() {
63
+ return 'ColorBuilder'
64
+ }
65
+
66
+
67
+ }
68
+
@@ -0,0 +1,80 @@
1
+ import { newNummery } from '../../support/nummery.js'
2
+ import { Proxies } from '../../support/proxies.js'
3
+ import { Utils } from '../../support/utils.js'
4
+ const { validateHex } = Utils
5
+
6
+ /**
7
+ * @file
8
+ * @imports ../typedefs.js
9
+ */
10
+ /**
11
+ * create a new FakeRgbColor instance
12
+ * @param {...any} args
13
+ * @returns {FakeRgbColor}
14
+ */
15
+ export const newFakeRgbColor = (...args) => {
16
+ return Proxies.guard(new FakeRgbColor(...args))
17
+ }
18
+
19
+
20
+ class FakeRgbColor {
21
+ constructor(color) {
22
+ this.__color = color
23
+ this.__type = newNummery('RGB')
24
+ }
25
+ toString() {
26
+ return 'RgbColor'
27
+ }
28
+
29
+ __checkHex() {
30
+ const v = validateHex(this.__color)
31
+ if (!v) throw this.__invalidArg(this.__color)
32
+ return v
33
+ }
34
+
35
+ __invalidArg(value) {
36
+ throw new Error(`Invalid argument ${value}`)
37
+ }
38
+ /**
39
+ * asHexString() https://developers.google.com/apps-script/reference/base/rgb-color.html#ashexstring
40
+ * Returns the color as a CSS-style 7 character hexadecimal string (#rrggbb) or 9 character hexadecimal string (#aarrggbb).
41
+ * @returns {string}
42
+ */
43
+ asHexString() {
44
+ return this.__checkHex().hex
45
+ }
46
+
47
+ /**
48
+ * getBlue() https://developers.google.com/apps-script/reference/base/rgb-color.html#getblue
49
+ * The blue channel of this color, as a number from 0 to 255.
50
+ * @returns {number}
51
+ */
52
+ getBlue() {
53
+ return this.__checkHex().b
54
+ }
55
+ /**
56
+ * getColorType() https://developers.google.com/apps-script/reference/base/rgb-color.html#getcolortype
57
+ * Get the type of this color.
58
+ * @returns {ColorType}
59
+ */
60
+ getColorType() {
61
+ return this.__type
62
+ }
63
+ /**
64
+ * getGreen() https://developers.google.com/apps-script/reference/base/rgb-color.html#getgreen
65
+ * The green channel of this color, as a number from 0 to 255.
66
+ * @returns {number}
67
+ */
68
+ getGreen() {
69
+ return this.__checkHex().g
70
+ }
71
+ /**
72
+ * getRed() https://developers.google.com/apps-script/reference/base/rgb-color.html#getred
73
+ * The red channel of this color, as a number from 0 to 255.
74
+ * @returns {number}
75
+ */
76
+ getRed() {
77
+ return this.__checkHex().r
78
+ }
79
+ }
80
+