Skip to content

Commit

Permalink
refactor: to use http-adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
tdeekens committed Jan 14, 2021
1 parent 4efed25 commit e9dd9bf
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 98 deletions.
2 changes: 1 addition & 1 deletion packages/application-shell/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"@emotion/react": "11.1.4",
"@emotion/styled": "11.0.0",
"@flopflip/combine-adapters": "0.0.10",
"@flopflip/graphql-adapter": "3.0.4",
"@flopflip/http-adapter": "0.0.1",
"@flopflip/launchdarkly-adapter": "5.0.4",
"@flopflip/memory-adapter": "3.0.8",
"@flopflip/react-broadcast": "12.1.6",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import type { TFlags, TGraphQLAdapterArgs } from '@flopflip/types';
import type { TFlags } from '@flopflip/types';
import type { TFetchLoggedInUserQuery } from '../../types/generated/mc';
import type { TAllFeaturesQuery } from '../../types/generated/ctp';

import React from 'react';
import ldAdapter from '@flopflip/launchdarkly-adapter';
import graphqlAdapter from '@flopflip/graphql-adapter';
import httpAdapter from '@flopflip/http-adapter';
import combineAdapters from '@flopflip/combine-adapters';
import { ConfigureFlopFlip } from '@flopflip/react-broadcast';
import { useApolloClient } from '@apollo/client/react';
import { GRAPHQL_TARGETS } from '@commercetools-frontend/constants';
import omitEmpty from 'omit-empty-es';
import useAllMenuFeatureToggles from '../../hooks/use-all-menu-feature-toggles';
import { getMcApiUrl } from '../../utils';
import { FLAGS } from '../../feature-toggles';
import AllFeaturesQuery from './fetch-all-features.mc.graphql';

Expand Down Expand Up @@ -41,29 +42,18 @@ type TFetchedGraphQLAdapterFlag = {
value: boolean;
reason?: string;
};
type TGraphQLAdapterRequestHeaders = {
'x-project-key': string;
'x-graphql-target': string;
};

// This value is hard-coded here because we want to make sure that the
// app uses our account of LD. The value is meant to be public, so there
// is no need to be concerned about security.
const ldClientSideIdProduction = '5979d95f6040390cd07b5e01';

combineAdapters.combine([ldAdapter, graphqlAdapter]);
combineAdapters.combine([ldAdapter, httpAdapter]);

function getRequestHeaders(adapterArgs: TGraphQLAdapterArgs<TGraphQlUser>) {
return omitEmpty<
TGraphQLAdapterRequestHeaders,
Partial<TGraphQLAdapterRequestHeaders>
>({
'x-project-key': adapterArgs.user.project,
'x-graphql-target': GRAPHQL_TARGETS.MERCHANT_CENTER_BACKEND,
});
}

function getLaunchDarklyUserCustomFields(user: Props['user'], projectKey?: string) {
function getLaunchDarklyUserCustomFields(
user: Props['user'],
projectKey?: string
) {
return omitEmpty<
TLaunchDarklyUserCustomFields,
Partial<TLaunchDarklyUserCustomFields>
Expand All @@ -77,7 +67,7 @@ function getLaunchDarklyUserCustomFields(user: Props['user'], projectKey?: strin
});
}

function getGraphQlUser(user: Props['user'], projectKey?: string) {
function getHttpUser(user: Props['user'], projectKey?: string) {
return omitEmpty<TGraphQlUser, Partial<TGraphQlUser>>({
key: user?.id,
project: projectKey,
Expand All @@ -99,6 +89,7 @@ const parseFlags = (fetchedFlags: TFetchedFlags) =>
);

export const SetupFlopFlipProvider = (props: Props) => {
const apolloClient = useApolloClient();
const allMenuFeatureToggles = useAllMenuFeatureToggles();
const flags = React.useMemo(
() => ({
Expand Down Expand Up @@ -141,17 +132,23 @@ export const SetupFlopFlipProvider = (props: Props) => {
custom: getLaunchDarklyUserCustomFields(props.user, props.projectKey),
},
},
graphql: {
user: getGraphQlUser(props.user, props.projectKey),
uri: `${getMcApiUrl()}/graphql`,
query: AllFeaturesQuery,
pollingInteralMs: 1000 * 60 * 30, // 30 minutes
getRequestHeaders,
parseFlags,
http: {
user: getHttpUser(props.user, props.projectKey),
execute: async () => {
const response = await apolloClient.query<TAllFeaturesQuery>({
query: AllFeaturesQuery,
errorPolicy: 'ignore',
context: {
target: GRAPHQL_TARGETS.COMMERCETOOLS_PLATFORM,
},
});

return parseFlags(response.data);
},
cacheIdentifier: 'local',
},
}),
[flags, props.ldClientSideId, props.projectKey, props.user]
[apolloClient, flags, props.ldClientSideId, props.projectKey, props.user]
);

if (process.env.NODE_ENV === 'test') {
Expand Down
110 changes: 41 additions & 69 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3797,6 +3797,16 @@
resolved "https://registry.yarnpkg.com/@exodus/schemasafe/-/schemasafe-1.0.0-rc.3.tgz#dda2fbf3dafa5ad8c63dadff7e01d3fdf4736025"
integrity sha512-GoXw0U2Qaa33m3eUcxuHnHpNvHjNlLo0gtV091XBpaRINaB4X6FGCG5XKxSFNFiPpugUDqNruHzaqpTdDm4AOg==

"@flopflip/adapter-utilities@1.0.7":
version "1.0.7"
resolved "https://registry.yarnpkg.com/@flopflip/adapter-utilities/-/adapter-utilities-1.0.7.tgz#474d5edefae650fce78bbce1945348aa539ff87c"
integrity sha512-2rls3Xentmw7CEMKaaofgYZ0wFh61dWDhOaxv4samsIwH9vAqoEVKHADycPEglLOClUCm46Nk42RqdYz1W4EtQ==
dependencies:
"@babel/runtime" "7.12.5"
"@flopflip/types" "4.1.5"
globalthis "1.0.1"
lodash "4.17.20"

"@flopflip/adapter-utilities@1.0.8":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@flopflip/adapter-utilities/-/adapter-utilities-1.0.8.tgz#2ec21832efd87e11f785908db0e67e87706edc42"
Expand All @@ -3818,16 +3828,16 @@
mitt "2.1.0"
tiny-warning "1.0.3"

"@flopflip/graphql-adapter@3.0.4":
version "3.0.4"
resolved "https://registry.yarnpkg.com/@flopflip/graphql-adapter/-/graphql-adapter-3.0.4.tgz#bda4f76720a03f3a2e7b709f0e6a6c7b7422e25c"
integrity sha512-ehEfcHkTLO41iihq5W7tJmlBmMVt2hDaGt/8PI/ZMRpSBZlNMjl5RRHzcKCZfdTYFDAwCDKwziHtXvlioy5dtQ==
"@flopflip/http-adapter@0.0.1":
version "0.0.1"
resolved "https://registry.yarnpkg.com/@flopflip/http-adapter/-/http-adapter-0.0.1.tgz#f18e2dc2b465c207e11a58d82586f56837257f76"
integrity sha512-l+oI7SCQJzimafKd8eaWV+lP1jIVtTGPSA8lBjfiLNK2rQmzvZKjDk0LaHTxudbs7Db+S0y+rzq7qq657hs8MQ==
dependencies:
"@babel/runtime" "7.12.5"
"@flopflip/adapter-utilities" "1.0.8"
"@flopflip/localstorage-cache" "1.0.9"
"@flopflip/sessionstorage-cache" "1.0.9"
"@flopflip/types" "4.1.6"
"@flopflip/adapter-utilities" "1.0.7"
"@flopflip/localstorage-cache" "1.0.8"
"@flopflip/sessionstorage-cache" "1.0.8"
"@flopflip/types" "4.1.5"
lodash "4.17.20"
mitt "2.1.0"
tiny-warning "1.0.3"
Expand All @@ -3847,12 +3857,12 @@
mitt "2.1.0"
tiny-warning "1.0.3"

"@flopflip/localstorage-cache@1.0.9":
version "1.0.9"
resolved "https://registry.yarnpkg.com/@flopflip/localstorage-cache/-/localstorage-cache-1.0.9.tgz#e3ca03741c1e1a187f84dde1e99ffa012c8c837c"
integrity sha512-syBAjurPdsQTeHIUHeFW94Yg3CmiKOa4Udgh82pyvhtQ85nWgyuRyKFyYzCGyUL7w6kITmhxbLi6iHmhzn/d5g==
"@flopflip/localstorage-cache@1.0.8":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@flopflip/localstorage-cache/-/localstorage-cache-1.0.8.tgz#a212d2a9f075b8a227b41fd98c2cc0b82416a87d"
integrity sha512-kybiG5Yzae008qWw354YQnQBhWEZ8fpiHov/DAjHrDn3lC65DKLVezP9DA4M0HPeb8AvtH+yOXFODC7WJeQLow==
dependencies:
"@flopflip/types" "4.1.6"
"@flopflip/types" "4.1.5"

"@flopflip/memory-adapter@3.0.8":
version "3.0.8"
Expand Down Expand Up @@ -3887,12 +3897,17 @@
react-is "17.0.1"
tiny-warning "1.0.3"

"@flopflip/sessionstorage-cache@1.0.9":
version "1.0.9"
resolved "https://registry.yarnpkg.com/@flopflip/sessionstorage-cache/-/sessionstorage-cache-1.0.9.tgz#6576403e0204624dcbd9ca5c9e4996140a87ac4c"
integrity sha512-kyFKm2/t/u9lnT2pVmv52zHKo5DMejCPuiIDqIuKbfGJrGUiu3xmytKuZwvb9TPYV1+gD8YwjSSYr1rysWl7kA==
"@flopflip/sessionstorage-cache@1.0.8":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@flopflip/sessionstorage-cache/-/sessionstorage-cache-1.0.8.tgz#9b062e24b69a14270592b1bf38b88e9a4e0c7367"
integrity sha512-oRICIcJDae4rs9BQ8/yMelRBdar+Qr4OIs+MtlVF8V9n98AYR3vlCya1Y1Sx2EAP5LRRzhzWzPcJPLmnWt6/Yg==
dependencies:
"@flopflip/types" "4.1.6"
"@flopflip/types" "4.1.5"

"@flopflip/types@4.1.5":
version "4.1.5"
resolved "https://registry.yarnpkg.com/@flopflip/types/-/types-4.1.5.tgz#a5d9177a13cb9381c48c30a8bb2ea46aa4457cf0"
integrity sha512-kvAQdaPdXToM+2fCUxT50VbTeUYOjQymUeJLC7wf9rJbuDy0C+6rxvHTt+09qknFe4F7F6dfFueMgLQS0QWE2A==

"@flopflip/types@4.1.6":
version "4.1.6"
Expand Down Expand Up @@ -6570,12 +6585,7 @@
"@sentry/utils" "5.29.2"
tslib "^1.9.3"

"@sentry/types@5.29.0":
version "5.29.0"
resolved "https://registry.yarnpkg.com/@sentry/types/-/types-5.29.0.tgz#af5cec98cde54316c14df3121f0e8106e56b578e"
integrity sha512-iDkxT/9sT3UF+Xb+JyLjZ5caMXsgLfRyV9VXQEiR2J6mgpMielj184d9jeF3bm/VMuAf/VFFqrHlcVsVgmrrMw==

"@sentry/types@5.29.2":
"@sentry/types@5.29.0", "@sentry/types@5.29.2":
version "5.29.2"
resolved "https://registry.yarnpkg.com/@sentry/types/-/types-5.29.2.tgz#ac87383df1222c2d9b9f8f9ed7a6b86ea41a098a"
integrity sha512-dM9wgt8wy4WRty75QkqQgrw9FV9F+BOMfmc0iaX13Qos7i6Qs2Q0dxtJ83SoR4YGtW8URaHzlDtWlGs5egBiMA==
Expand Down Expand Up @@ -8371,7 +8381,7 @@ ansi-regex@^3.0.0:
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-3.0.0.tgz#ed0317c322064f79466c02966bddb605ab37d998"
integrity sha1-7QMXwyIGT3lGbAKWa922Bas32Zg=

ansi-regex@^4.0.0, ansi-regex@^4.1.0:
ansi-regex@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-4.1.0.tgz#8b9f8f08cf1acb843756a839ca8c7e3168c51997"
integrity sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==
Expand Down Expand Up @@ -14973,7 +14983,7 @@ gatsby-legacy-polyfills@^0.4.0:
dependencies:
core-js-compat "^3.6.5"

gatsby-link@^2.8.0:
gatsby-link@2.8.0, gatsby-link@^2.8.0:
version "2.8.0"
resolved "https://registry.yarnpkg.com/gatsby-link/-/gatsby-link-2.8.0.tgz#ca0e32eec9d6adc91d7d515ce6d8b38c76bfd9dc"
integrity sha512-b+VvvsnIyukn9asDa2U9I8zjq3SL6KX53Ln7KwR0qzNwtK//GSTIifrjREtTLS3xBbwCRKkZ2eVmc5NWeNkiAw==
Expand Down Expand Up @@ -16355,23 +16365,13 @@ graphql-ws@3.1.0:
resolved "https://registry.yarnpkg.com/graphql-ws/-/graphql-ws-3.1.0.tgz#cd09d385a21ab88af4c226da79c19351df9b27e8"
integrity sha512-zbex3FSiFz0iRgfkzDNWpOY/sYWoX+iZ5XUhakaDwOh99HSuk8rPt5suuxdXUVzEg5TGQ9rwzNaz/+mTPtS0yg==

graphql@14.7.0, graphql@^14.6.0:
graphql@14.7.0, graphql@15.3.0, graphql@^14.6.0, graphql@^15.4.0:
version "14.7.0"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-14.7.0.tgz#7fa79a80a69be4a31c27dda824dc04dac2035a72"
integrity sha512-l0xWZpoPKpppFzMfvVyFmp9vLN7w/ZZJPefUicMCepfJeQ8sMcztloGYY9DfjVPo6tIUDzU5Hw3MUbIjj9AVVA==
dependencies:
iterall "^1.2.2"

graphql@15.3.0:
version "15.3.0"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-15.3.0.tgz#3ad2b0caab0d110e3be4a5a9b2aa281e362b5278"
integrity sha512-GTCJtzJmkFLWRfFJuoo9RWWa/FfamUHgiFosxi/X1Ani4AVWbeyBenZTNX6dM+7WSbbFfTo/25eh0LLkwHMw2w==

graphql@^15.4.0:
version "15.4.0"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-15.4.0.tgz#e459dea1150da5a106486ba7276518b5295a4347"
integrity sha512-EB3zgGchcabbsU9cFe1j+yxdzKQKAbGUWRb13DsrsMN1yyfmmIq+2+L5MqVWcDCE4V89R5AyUOi7sMOGxdsYtA==

gray-matter@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/gray-matter/-/gray-matter-4.0.2.tgz#9aa379e3acaf421193fce7d2a28cebd4518ac454"
Expand Down Expand Up @@ -17496,15 +17496,7 @@ intersection-observer@0.12.0:
resolved "https://registry.yarnpkg.com/intersection-observer/-/intersection-observer-0.12.0.tgz#6c84628f67ce8698e5f9ccf857d97718745837aa"
integrity sha512-2Vkz8z46Dv401zTWudDGwO7KiGHNDkMv417T5ItcNYfmvHR/1qCTVBO9vwH8zZmQ0WkA/1ARwpysR9bsnop4NQ==

intl-messageformat-parser@6.1.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/intl-messageformat-parser/-/intl-messageformat-parser-6.1.0.tgz#134328151c41592d9e1a61f5c6779c06c8eb3f08"
integrity sha512-nPPh2kOrKqlh4D9bCAetxkrUiq5/6S1exPQyg52Ihusy0ECNGhZ0Qmq8pFRK9gWIuiQPVmLA7eSNp8diC2tX3w==
dependencies:
"@formatjs/ecma402-abstract" "1.5.0"
tslib "^2.0.1"

intl-messageformat-parser@6.1.2:
intl-messageformat-parser@6.1.0, intl-messageformat-parser@6.1.2:
version "6.1.2"
resolved "https://registry.yarnpkg.com/intl-messageformat-parser/-/intl-messageformat-parser-6.1.2.tgz#28c65f3689f538e66c7cf628881548d6a82ff3c2"
integrity sha512-4GQDEPhl/ZMNDKwMsLqyw1LG2IAWjmLJXdmnRcHKeLQzpgtNYZI6lVw1279pqIkRk2MfKb9aDsVFzm565azK5A==
Expand Down Expand Up @@ -24285,27 +24277,7 @@ pretty-error@^2.1.1:
lodash "^4.17.20"
renderkid "^2.0.4"

pretty-format@^24.9.0:
version "24.9.0"
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-24.9.0.tgz#12fac31b37019a4eea3c11aa9a959eb7628aa7c9"
integrity sha512-00ZMZUiHaJrNfk33guavqgvfJS30sLYf0f8+Srklv0AMPodGGHcoHgksZ3OThYnIvOd+8yMCn0YiEOogjlgsnA==
dependencies:
"@jest/types" "^24.9.0"
ansi-regex "^4.0.0"
ansi-styles "^3.2.0"
react-is "^16.8.4"

pretty-format@^25.5.0:
version "25.5.0"
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-25.5.0.tgz#7873c1d774f682c34b8d48b6743a2bf2ac55791a"
integrity sha512-kbo/kq2LQ/A/is0PQwsEHM7Ca6//bGPPvU6UnsdDRSKTWxT/ru/xb88v4BJf6a69H+uTytOEsTusT9ksd/1iWQ==
dependencies:
"@jest/types" "^25.5.0"
ansi-regex "^5.0.0"
ansi-styles "^4.0.0"
react-is "^16.12.0"

pretty-format@^26.0.0, pretty-format@^26.6.2:
pretty-format@26.6.2, pretty-format@^24.9.0, pretty-format@^25.5.0, pretty-format@^26.0.0, pretty-format@^26.6.2:
version "26.6.2"
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-26.6.2.tgz#e35c2705f14cb7fe2fe94fa078345b444120fc93"
integrity sha512-7AeGuCYNGmycyQbCqd/3PWH4eOoX/OiCa0uphp57NVTeAGdJGaAliecxwBDHYQCIvrW7aDBZCYeNTP/WX69mkg==
Expand Down Expand Up @@ -25033,7 +25005,7 @@ react-intl@5.10.9, react-intl@^5.7.0:
shallow-equal "^1.2.1"
tslib "^2.0.1"

react-is@16.13.1, react-is@^16.12.0, react-is@^16.13.1, react-is@^16.6.0, react-is@^16.7.0, react-is@^16.8.1, react-is@^16.8.4, react-is@^16.8.6:
react-is@16.13.1, react-is@^16.12.0, react-is@^16.13.1, react-is@^16.6.0, react-is@^16.7.0, react-is@^16.8.1, react-is@^16.8.6:
version "16.13.1"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==
Expand Down Expand Up @@ -26689,7 +26661,7 @@ shallowequal@^1.1.0:
resolved "https://registry.yarnpkg.com/shallowequal/-/shallowequal-1.1.0.tgz#188d521de95b9087404fd4dcb68b13df0ae4e7f8"
integrity sha512-y0m1JoUZSlPAjXVtPPW70aZWfIL/dSP7AFkRnniLCrK/8MDKog3TySTBmckD+RObVxH0v4Tox67+F14PdED2oQ==

sharp@^0.26.3:
sharp@0.26.3, sharp@^0.26.3:
version "0.26.3"
resolved "https://registry.yarnpkg.com/sharp/-/sharp-0.26.3.tgz#9de8577a986b22538e6e12ced1f7e8a53f9728de"
integrity sha512-NdEJ9S6AMr8Px0zgtFo1TJjMK/ROMU92MkDtYn2BBrDjIx3YfH9TUyGdzPC+I/L619GeYQc690Vbaxc5FPCCWg==
Expand Down

0 comments on commit e9dd9bf

Please sign in to comment.