How to use @shopify/network - 10 common examples

To help you get started, we’ve selected a few @shopify/network examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Shopify / quilt / packages / koa-shopify-auth / src / verify-request / verify-token.ts View on Github external
return async function verifyTokenMiddleware(
    ctx: Context,
    next: NextFunction,
  ) {
    const {session} = ctx;

    if (session && session.accessToken) {
      ctx.cookies.set(TOP_LEVEL_OAUTH_COOKIE_NAME);
      // If a user has installed the store previously on their shop, the accessToken can be stored in session.
      // we need to check if the accessToken is valid, and the only way to do this is by hitting the api.
      const response = await fetch(
        `https://${session.shop}/admin/metafields.json`,
        {
          method: Method.Post,
          headers: {
            [Header.ContentType]: 'application/json',
            'X-Shopify-Access-Token': session.accessToken,
          },
        },
      );

      if (response.status === StatusCode.Unauthorized) {
        redirectToAuth(routes, ctx);
        return;
      }

      await next();
      return;
    }

    ctx.cookies.set(TEST_COOKIE_NAME, '1');
github Shopify / quilt / packages / koa-shopify-auth / src / verify-request / verify-token.ts View on Github external
return async function verifyTokenMiddleware(
    ctx: Context,
    next: NextFunction,
  ) {
    const {session} = ctx;

    if (session && session.accessToken) {
      ctx.cookies.set(TOP_LEVEL_OAUTH_COOKIE_NAME);
      // If a user has installed the store previously on their shop, the accessToken can be stored in session.
      // we need to check if the accessToken is valid, and the only way to do this is by hitting the api.
      const response = await fetch(
        `https://${session.shop}/admin/metafields.json`,
        {
          method: Method.Post,
          headers: {
            [Header.ContentType]: 'application/json',
            'X-Shopify-Access-Token': session.accessToken,
          },
        },
      );

      if (response.status === StatusCode.Unauthorized) {
        redirectToAuth(routes, ctx);
        return;
      }

      await next();
      return;
    }
github Shopify / quilt / packages / react-performance / src / performance-report.ts View on Github external
timeout.current = setTimeout(async () => {
      if (timeout.current) {
        clearTimeout(timeout.current);
        timeout.current = undefined;
      }

      try {
        await fetch(url, {
          method: Method.Post,
          headers: {
            [Header.ContentType]: 'application/json',
          },
          body: JSON.stringify({
            connection: serializableClone((navigator as any).connection),
            events: events.current,
            navigations: navigations.current.map(navigation => ({
              details: navigation.toJSON({removeEventMetadata: false}),
              metadata: navigation.metadata,
            })),
            pathname: window.location.pathname,
          }),
        });
      } catch (error) {
        if (onError) {
          onError(error);
        }
      } finally {
github Shopify / quilt / packages / koa-shopify-webhooks / src / register.ts View on Github external
export async function registerWebhook({
  address,
  topic,
  accessToken,
  shop,
  apiVersion,
}: Options) {
  const response = await fetch(
    `https://${shop}/admin/api/${apiVersion}/graphql.json`,
    {
      method: Method.Post,
      body: buildQuery(topic, address),
      headers: {
        [WebhookHeader.AccessToken]: accessToken,
        [Header.ContentType]: 'application/graphql',
      },
    },
  );

  const result = await response.json();

  if (
    result.data &&
    result.data.webhookSubscriptionCreate &&
    result.data.webhookSubscriptionCreate.webhookSubscription
  ) {
    return {success: true, result};
  } else {
    return {success: false, result};
  }
}
github Shopify / quilt / packages / koa-shopify-webhooks / src / register.ts View on Github external
export async function registerWebhook({
  address,
  topic,
  accessToken,
  shop,
  apiVersion,
}: Options) {
  const response = await fetch(
    `https://${shop}/admin/api/${apiVersion}/graphql.json`,
    {
      method: Method.Post,
      body: buildQuery(topic, address),
      headers: {
        [WebhookHeader.AccessToken]: accessToken,
        [Header.ContentType]: 'application/graphql',
      },
    },
  );

  const result = await response.json();

  if (
    result.data &&
    result.data.webhookSubscriptionCreate &&
    result.data.webhookSubscriptionCreate.webhookSubscription
  ) {
    return {success: true, result};
github Shopify / quilt / packages / react-performance / src / performance-report.ts View on Github external
timeout.current = setTimeout(async () => {
      if (timeout.current) {
        clearTimeout(timeout.current);
        timeout.current = undefined;
      }

      try {
        await fetch(url, {
          method: Method.Post,
          headers: {
            [Header.ContentType]: 'application/json',
          },
          body: JSON.stringify({
            connection: serializableClone((navigator as any).connection),
            events: events.current,
            navigations: navigations.current.map(navigation => ({
              details: navigation.toJSON({removeEventMetadata: false}),
              metadata: navigation.metadata,
            })),
            pathname: window.location.pathname,
          }),
        });
      } catch (error) {
        if (onError) {
          onError(error);
github Shopify / quilt / packages / sewing-kit-koa / src / middleware.ts View on Github external
async function sewingKitMiddleware(ctx: Context, next: () => Promise) {
    const assets = new Assets({
      assetPrefix,
      userAgent: ctx.get(Header.UserAgent),
      manifestPath,
    });

    setAssets(ctx, assets);

    await next();
  }
github Shopify / quilt / packages / koa-performance / src / middleware.ts View on Github external
const {body} = ctx.request as any;
      if (!isClientMetricsBody(body)) {
        ctx.status = StatusCode.UnprocessableEntity;
        return;
      }

      const statsd = new StatsDClient({
        host: statsdHost,
        port: statsdPort,
        logger: statsLogger,
        snakeCase: true,
        prefix,
      });

      const userAgent = ctx.get(Header.UserAgent);
      const {connection, events, navigations} = body;

      const metrics: {
        name: string;
        value: any;
        tags: {[key: string]: string | undefined | null};
      }[] = [];

      const additionalTags = getAdditionalTags
        ? getAdditionalTags(body, userAgent)
        : {};

      const tags = {
        browserConnectionType: connection.effectiveType,
        ...additionalTags,
      };
github Shopify / quilt / packages / react-network / src / components / BaseUri.tsx View on Github external
export default function BaseUri({uri}: Props) {
  useCspDirective(CspDirective.BaseUri, uri);
  return null;
}
github Shopify / quilt / packages / react-network / src / components / BlockAllMixedContent.tsx View on Github external
export default function BlockAllMixedContent({value = true}: Props) {
  useCspDirective(CspDirective.BlockAllMixedContent, value);
  return null;
}