1 | /*
|
2 | * Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
3 | *
|
4 | * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
|
5 | * the License. A copy of the License is located at
|
6 | *
|
7 | * http://aws.amazon.com/apache2.0/
|
8 | *
|
9 | * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
10 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
|
11 | * and limitations under the License.
|
12 | */
|
13 |
|
14 | let dataMemory = {};
|
15 |
|
16 | /** @class */
|
17 | export class MemoryStorage {
|
18 | /**
|
19 | * This is used to set a specific item in storage
|
20 | * @param {string} key - the key for the item
|
21 | * @param {object} value - the value
|
22 | * @returns {string} value that was set
|
23 | */
|
24 | static setItem(key: string, value: any) {
|
25 | dataMemory[key] = value;
|
26 | return dataMemory[key];
|
27 | }
|
28 |
|
29 | /**
|
30 | * This is used to get a specific key from storage
|
31 | * @param {string} key - the key for the item
|
32 | * This is used to clear the storage
|
33 | * @returns {string} the data item
|
34 | */
|
35 | static getItem(key: string) {
|
36 | return Object.prototype.hasOwnProperty.call(dataMemory, key)
|
37 | ? dataMemory[key]
|
38 | : undefined;
|
39 | }
|
40 |
|
41 | /**
|
42 | * This is used to remove an item from storage
|
43 | * @param {string} key - the key being set
|
44 | * @returns {string} value - value that was deleted
|
45 | */
|
46 | static removeItem(key: string) {
|
47 | return delete dataMemory[key];
|
48 | }
|
49 |
|
50 | /**
|
51 | * This is used to clear the storage
|
52 | * @returns {string} nothing
|
53 | */
|
54 | static clear() {
|
55 | dataMemory = {};
|
56 | return dataMemory;
|
57 | }
|
58 | }
|
59 |
|
60 | export class StorageHelper {
|
61 | private storageWindow: any;
|
62 | /**
|
63 | * This is used to get a storage object
|
64 | * @returns {object} the storage
|
65 | */
|
66 | constructor() {
|
67 | try {
|
68 | this.storageWindow = window.localStorage;
|
69 | this.storageWindow.setItem('aws.amplify.test-ls', 1);
|
70 | this.storageWindow.removeItem('aws.amplify.test-ls');
|
71 | } catch (exception) {
|
72 | this.storageWindow = MemoryStorage;
|
73 | }
|
74 | }
|
75 |
|
76 | /**
|
77 | * This is used to return the storage
|
78 | * @returns {object} the storage
|
79 | */
|
80 | getStorage(): any {
|
81 | return this.storageWindow;
|
82 | }
|
83 | }
|
84 |
|
85 | /**
|
86 | * @deprecated use named import
|
87 | */
|
88 | export default StorageHelper;
|