sharetribe-flex-sdk 1.14.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.
Files changed (87) hide show
  1. package/.circleci/config.yml +22 -0
  2. package/.eslintignore +3 -0
  3. package/.eslintrc.js +19 -0
  4. package/CHANGELOG.md +231 -0
  5. package/LICENSE +201 -0
  6. package/README.md +76 -0
  7. package/build/sharetribe-flex-sdk-node.js +13592 -0
  8. package/build/sharetribe-flex-sdk-web.js +1 -0
  9. package/docs/README.md +20 -0
  10. package/docs/authentication.md +179 -0
  11. package/docs/calling-the-api.md +217 -0
  12. package/docs/configurations.md +95 -0
  13. package/docs/developing-sdk.md +131 -0
  14. package/docs/docpress.json +14 -0
  15. package/docs/features.md +14 -0
  16. package/docs/keep-alive.md +48 -0
  17. package/docs/object-query-parameters.md +36 -0
  18. package/docs/scripts.js +41 -0
  19. package/docs/serializing-types-to-json.md +40 -0
  20. package/docs/sharing-session-between-client-and-server.md +19 -0
  21. package/docs/styles.css +95 -0
  22. package/docs/token-store.md +114 -0
  23. package/docs/try-it-in-browser.md +32 -0
  24. package/docs/try-it-in-the-playground.md +153 -0
  25. package/docs/types.md +27 -0
  26. package/docs/writing-your-own-token-store.md +29 -0
  27. package/docs/your-own-types.md +61 -0
  28. package/examples/README.md +5 -0
  29. package/examples/getting-started-browser/README.md +22 -0
  30. package/examples/getting-started-browser/index.html +89 -0
  31. package/examples/getting-started-browser/index.js +156 -0
  32. package/examples/getting-started-browser/screenshots/screenshot1.png +0 -0
  33. package/examples/getting-started-browser/screenshots/screenshot2.png +0 -0
  34. package/examples/getting-started-node/README.md +23 -0
  35. package/examples/getting-started-node/index.js +139 -0
  36. package/examples/getting-started-node/screenshots/screenshot.png +0 -0
  37. package/package.json +83 -0
  38. package/playground.js +295 -0
  39. package/src/browser_cookie_store.js +26 -0
  40. package/src/context_runner.js +151 -0
  41. package/src/context_runner.test.js +185 -0
  42. package/src/detect.js +11 -0
  43. package/src/express_cookie_store.js +57 -0
  44. package/src/fake/adapter.js +130 -0
  45. package/src/fake/api.js +137 -0
  46. package/src/fake/auth.js +84 -0
  47. package/src/fake/token_store.js +231 -0
  48. package/src/index.js +25 -0
  49. package/src/interceptors/.eslintrc.js +5 -0
  50. package/src/interceptors/add_auth_header.js +32 -0
  51. package/src/interceptors/add_auth_header.test.js +50 -0
  52. package/src/interceptors/add_auth_token_response.js +16 -0
  53. package/src/interceptors/add_client_id_to_params.js +12 -0
  54. package/src/interceptors/add_client_secret_to_params.js +15 -0
  55. package/src/interceptors/add_grant_type_to_params.js +23 -0
  56. package/src/interceptors/add_idp_client_id_to_params.js +17 -0
  57. package/src/interceptors/add_idp_id_to_params.js +17 -0
  58. package/src/interceptors/add_idp_token_to_params.js +17 -0
  59. package/src/interceptors/add_scope_to_params.js +18 -0
  60. package/src/interceptors/add_subject_token_to_params.js +18 -0
  61. package/src/interceptors/add_token_exchange_grant_type_to_params.js +12 -0
  62. package/src/interceptors/auth_info.js +50 -0
  63. package/src/interceptors/clear_token_after_revoke.js +45 -0
  64. package/src/interceptors/default_params.js +12 -0
  65. package/src/interceptors/fetch_auth_token_from_api.js +33 -0
  66. package/src/interceptors/fetch_auth_token_from_store.js +27 -0
  67. package/src/interceptors/fetch_refresh_token_for_revoke.js +24 -0
  68. package/src/interceptors/multipart_request.js +35 -0
  69. package/src/interceptors/retry_with_anon_token.js +58 -0
  70. package/src/interceptors/retry_with_refresh_token.js +70 -0
  71. package/src/interceptors/save_token.js +20 -0
  72. package/src/interceptors/transit_request.js +27 -0
  73. package/src/interceptors/transit_request.test.js +58 -0
  74. package/src/interceptors/transit_response.js +27 -0
  75. package/src/memory_store.js +19 -0
  76. package/src/params_serializer.js +65 -0
  77. package/src/params_serializer.test.js +58 -0
  78. package/src/sdk.js +894 -0
  79. package/src/sdk.test.js +908 -0
  80. package/src/serializer.js +279 -0
  81. package/src/serializer.test.js +229 -0
  82. package/src/token_store.js +15 -0
  83. package/src/types.js +108 -0
  84. package/src/types.test.js +75 -0
  85. package/src/utils.js +68 -0
  86. package/src/utils.test.js +85 -0
  87. package/webpack.config.babel.js +47 -0
package/src/utils.js ADDED
@@ -0,0 +1,68 @@
1
+ import _ from 'lodash';
2
+
3
+ /**
4
+ Take URL and remove the trailing slashes.
5
+
6
+ Example:
7
+
8
+ ```
9
+ trimEndSlash("http://www.api.com") => "http://www.api.com"
10
+ trimEndSlash("http://www.api.com/") => "http://www.api.com"
11
+ trimEndSlash("http://www.api.com//") => "http://www.api.com"
12
+ ```
13
+ */
14
+ export const trimEndSlash = url => _.trimEnd(url, '/');
15
+
16
+ export const fnPath = path =>
17
+ _.without(path.split('/'), '').map(part => part.replace(/_\w/g, m => m[1].toUpperCase()));
18
+
19
+ export const formData = params =>
20
+ _.reduce(
21
+ params,
22
+ (pairs, v, k) => {
23
+ pairs.push(`${encodeURIComponent(k)}=${encodeURIComponent(v)}`);
24
+ return pairs;
25
+ },
26
+ []
27
+ ).join('&');
28
+
29
+ /**
30
+ Serialize a single attribute in an object query parameter.
31
+ */
32
+ const serializeAttribute = attribute => {
33
+ if (_.isPlainObject(attribute)) {
34
+ throw new Error('Nested object in query parameter.');
35
+ } else if (Array.isArray(attribute)) {
36
+ return attribute.join(',');
37
+ } else {
38
+ return attribute;
39
+ }
40
+ };
41
+
42
+ /**
43
+ Serializes an object into a Flex API query parameter value format. Null and
44
+ undefined object attributes are dropped.
45
+
46
+ Example:
47
+
48
+ {
49
+ a: 'foo',
50
+ b: '150',
51
+ c: null,
52
+ d: ['foo', 'bar'],
53
+ }
54
+
55
+ =>
56
+
57
+ 'a:foo;b:150;d:foo,bar'
58
+ */
59
+ export const objectQueryString = obj => {
60
+ if (!_.isPlainObject(obj)) {
61
+ throw new Error('Parameter not an object.');
62
+ }
63
+
64
+ return Object.entries(obj)
65
+ .filter(([, v]) => v !== null && v !== undefined)
66
+ .map(([k, v]) => `${k}:${serializeAttribute(v)}`)
67
+ .join(';');
68
+ };
@@ -0,0 +1,85 @@
1
+ import { fnPath, trimEndSlash, formData, objectQueryString } from './utils';
2
+
3
+ describe('utils', () => {
4
+ describe('pathToMethodName', () => {
5
+ it('takes URL path, returns method name', () => {
6
+ expect(fnPath('users')).toEqual(['users']);
7
+ expect(fnPath('/users')).toEqual(['users']);
8
+ expect(fnPath('users/')).toEqual(['users']);
9
+ expect(fnPath('/users/')).toEqual(['users']);
10
+ expect(fnPath('/users/create/')).toEqual(['users', 'create']);
11
+ });
12
+
13
+ it('camelizes it', () => {
14
+ expect(fnPath('/listings/upload_image')).toEqual(['listings', 'uploadImage']);
15
+ expect(fnPath('/listings/upload_new_awesome_image')).toEqual([
16
+ 'listings',
17
+ 'uploadNewAwesomeImage',
18
+ ]);
19
+ });
20
+ });
21
+
22
+ describe('trimEndSlash', () => {
23
+ it('trims trailing slashes', () => {
24
+ expect(trimEndSlash('http://www.api.com')).toEqual('http://www.api.com');
25
+ expect(trimEndSlash('http://www.api.com/')).toEqual('http://www.api.com');
26
+ expect(trimEndSlash('http://www.api.com//')).toEqual('http://www.api.com');
27
+ });
28
+ });
29
+
30
+ describe('formData', () => {
31
+ it('encodes params to formData', () => {
32
+ expect(
33
+ formData({ username: 'joe.dunphy@example.com', password: '}4$3.872487=3&&]/6?.' })
34
+ ).toEqual('username=joe.dunphy%40example.com&password=%7D4%243.872487%3D3%26%26%5D%2F6%3F.');
35
+ });
36
+ });
37
+
38
+ describe('objectQueryString', () => {
39
+ it('constructs a valid URL string', () => {
40
+ expect(
41
+ objectQueryString({
42
+ w: 500,
43
+ h: 2000,
44
+ fit: 'scale',
45
+ newparam: 'newvalue',
46
+ a: true,
47
+ b: false,
48
+ })
49
+ ).toEqual('w:500;h:2000;fit:scale;newparam:newvalue;a:true;b:false');
50
+ });
51
+
52
+ it('drops null and undefined values', () => {
53
+ expect(
54
+ objectQueryString({
55
+ w: 500,
56
+ h: undefined,
57
+ fit: 'scale',
58
+ newparam: 'newvalue',
59
+ a: null,
60
+ })
61
+ ).toEqual('w:500;fit:scale;newparam:newvalue');
62
+ });
63
+
64
+ it('serializes array values', () => {
65
+ expect(
66
+ objectQueryString({
67
+ has_all: ['brakes', 'steering', true, 10],
68
+ foo: 'bar',
69
+ newparam: 'newvalue',
70
+ })
71
+ ).toEqual('has_all:brakes,steering,true,10;foo:bar;newparam:newvalue');
72
+ });
73
+
74
+ it('only takes an object', () => {
75
+ expect(() => objectQueryString('foo')).toThrowError('Parameter not an object.');
76
+ expect(() => objectQueryString(null)).toThrowError('Parameter not an object.');
77
+ });
78
+
79
+ it('does not allow nested objects', () => {
80
+ expect(() => objectQueryString({ foo: { nested: 'value' } })).toThrowError(
81
+ 'Nested object in query parameter.'
82
+ );
83
+ });
84
+ });
85
+ });
@@ -0,0 +1,47 @@
1
+ /* eslint-env node */
2
+ const path = require('path');
3
+ const webpack = require('webpack');
4
+
5
+ // Shared configs
6
+ const entry = './src/index.js';
7
+
8
+ const babelLoader = {
9
+ test: /.js$/,
10
+ exclude: [/node_modules/],
11
+ use: 'babel-loader',
12
+ };
13
+
14
+ const module = {
15
+ rules: [babelLoader],
16
+ };
17
+
18
+ const output = target => ({
19
+ path: path.resolve(__dirname, 'build'),
20
+ filename: `sharetribe-flex-sdk-${target}.js`,
21
+ library: 'sharetribeSdk',
22
+ libraryTarget: 'umd',
23
+ });
24
+
25
+ // Node configs
26
+ const nodeConfig = {
27
+ entry,
28
+ output: output('node'),
29
+ target: 'node',
30
+ module,
31
+ externals: [
32
+ 'axios',
33
+ ],
34
+ };
35
+
36
+ // Web configs
37
+ const webConfig = {
38
+ entry,
39
+ output: output('web'),
40
+ target: 'web',
41
+ module,
42
+ plugins: [
43
+ new webpack.optimize.UglifyJsPlugin(),
44
+ ],
45
+ };
46
+
47
+ export default () => ([nodeConfig, webConfig]);