Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #91 from Shopify/webhook_export_refactorings
Browse files Browse the repository at this point in the history
Separating webhook types into their own file
  • Loading branch information
paulomarg authored Feb 4, 2021
2 parents 46d917b + 3b5bed8 commit 0cdbccf
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 98 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Webhooks types are now exported outside the library [#91](https://github.com/shopify/shopify-node-api/pull/91)
### Fixed

## [0.3.1] - 2021-02-03
Expand Down
30 changes: 30 additions & 0 deletions src/base_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {SessionStorage} from './auth/session';

export interface ContextParams {
API_KEY: string;
API_SECRET_KEY: string;
SCOPES: string[];
HOST_NAME: string;
API_VERSION: ApiVersion;
IS_EMBEDDED_APP: boolean;
SESSION_STORAGE?: SessionStorage;
}

export enum ApiVersion {
April19 = '2019-04',
July19 = '2019-07',
October19 = '2019-10',
January20 = '2020-01',
April20 = '2020-04',
July20 = '2020-07',
October20 = '2020-10',
Unstable = 'unstable',
Unversioned = 'unversioned',
}

export enum ShopifyHeader {
AccessToken = 'X-Shopify-Access-Token',
Hmac = 'X-Shopify-Hmac-Sha256',
Topic = 'X-Shopify-Topic',
Domain = 'X-Shopify-Shop-Domain',
}
2 changes: 1 addition & 1 deletion src/clients/graphql/graphql_client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Context} from '../../context';
import {ShopifyHeader} from '../../types';
import {ShopifyHeader} from '../../base_types';
import {HttpClient} from '../http_client/http_client';
import {DataType, RequestReturn} from '../http_client/types';

Expand Down
2 changes: 1 addition & 1 deletion src/clients/graphql/test/graphql_client.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import '../../../test/test_helper';
import {ShopifyHeader} from '../../../types';
import {ShopifyHeader} from '../../../base_types';
import {assertHttpRequest} from '../../http_client/test/test_helper';
import {GraphqlClient} from '../graphql_client';

Expand Down
2 changes: 1 addition & 1 deletion src/clients/rest/rest_client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import querystring from 'querystring';

import {Context} from '../../context';
import {ShopifyHeader} from '../../types';
import {ShopifyHeader} from '../../base_types';
import {HttpClient} from '../http_client/http_client';
import {RequestParams, GetRequestParams} from '../http_client/types';

Expand Down
2 changes: 1 addition & 1 deletion src/clients/rest/test/rest_client.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import '../../../test/test_helper';
import querystring from 'querystring';

import {ShopifyHeader} from '../../../types';
import {ShopifyHeader} from '../../../base_types';
import {DataType, GetRequestParams} from '../../http_client/types';
import {assertHttpRequest} from '../../http_client/test/test_helper';
import {RestClient} from '../rest_client';
Expand Down
2 changes: 1 addition & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as ShopifyErrors from './error';
import {SessionStorage, MemorySessionStorage} from './auth/session';
import {ApiVersion, ContextParams} from './types';
import {ApiVersion, ContextParams} from './base_types';

interface ContextInterface extends ContextParams {
SESSION_STORAGE: SessionStorage;
Expand Down
2 changes: 1 addition & 1 deletion src/test/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Cookies from 'cookies';

import * as ShopifyErrors from '../error';
import {Context} from '../context';
import {ApiVersion, ContextParams} from '../types';
import {ApiVersion, ContextParams} from '../base_types';

jest.mock('cookies');

Expand Down
2 changes: 1 addition & 1 deletion src/test/test_helper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {enableFetchMocks} from 'jest-fetch-mock';

import {Context} from '../context';
import {ApiVersion} from '../types';
import {ApiVersion} from '../base_types';
import {MemorySessionStorage} from '../auth/session';

enableFetchMocks();
Expand Down
33 changes: 2 additions & 31 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,4 @@
import {SessionStorage} from './auth/session';

export interface ContextParams {
API_KEY: string;
API_SECRET_KEY: string;
SCOPES: string[];
HOST_NAME: string;
API_VERSION: ApiVersion;
IS_EMBEDDED_APP: boolean;
SESSION_STORAGE?: SessionStorage;
}

export enum ApiVersion {
April19 = '2019-04',
July19 = '2019-07',
October19 = '2019-10',
January20 = '2020-01',
April20 = '2020-04',
July20 = '2020-07',
October20 = '2020-10',
Unstable = 'unstable',
Unversioned = 'unversioned',
}

export enum ShopifyHeader {
AccessToken = 'X-Shopify-Access-Token',
Hmac = 'X-Shopify-Hmac-Sha256',
Topic = 'X-Shopify-Topic',
Domain = 'X-Shopify-Shop-Domain',
}

export * from './base_types';
export * from './auth/oauth/types';
export * from './clients/types';
export * from './webhooks/types';
68 changes: 10 additions & 58 deletions src/webhooks/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,20 @@ import {createHmac} from 'crypto';
import {StatusCode} from '@shopify/network';

import {GraphqlClient} from '../clients/graphql/graphql_client';
import {ShopifyHeader, ApiVersion} from '../types';
import {ShopifyHeader} from '../base_types';
import ShopifyUtilities from '../utils';
import {Context} from '../context';
import * as ShopifyErrors from '../error';

export enum DeliveryMethod {
Http = 'http',
EventBridge = 'eventbridge',
}

export type WebhookHandlerFunction = (
topic: string,
shop_domain: string,
body: Buffer
) => void;

export interface RegisterOptions {
// See https://shopify.dev/docs/admin-api/graphql/reference/events/webhooksubscriptiontopic for available topics
topic: string;
path: string;
shop: string;
accessToken: string;
apiVersion: ApiVersion;
deliveryMethod?: DeliveryMethod;
webhookHandler: WebhookHandlerFunction;
}

export interface RegisterReturn {
success: boolean;
result: unknown;
}

interface WebhookRegistryEntry {
path: string;
topic: string;
webhookHandler: WebhookHandlerFunction;
}

export interface ProcessOptions {
headers: Record<string, string>;
body: Buffer;
}

export interface ProcessReturn {
statusCode: StatusCode;
headers: Record<string, string>;
}

interface WebhookCheckResponseNode {
node: {
id: string;
callbackUrl: string;
};
}

interface WebhookCheckResponse {
data: {
webhookSubscriptions: {
edges: WebhookCheckResponseNode[];
};
};
}
import {
DeliveryMethod,
RegisterOptions,
RegisterReturn,
WebhookRegistryEntry,
ProcessOptions,
ProcessReturn,
WebhookCheckResponse,
} from './types';

interface RegistryInterface {
webhookRegistry: WebhookRegistryEntry[];
Expand Down
4 changes: 2 additions & 2 deletions src/webhooks/test/registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {createHmac} from 'crypto';

import {Method, Header, StatusCode} from '@shopify/network';

import {DeliveryMethod, ProcessReturn, RegisterOptions} from '../registry';
import {ApiVersion, ShopifyHeader} from '../../types';
import {DeliveryMethod, ProcessReturn, RegisterOptions} from '../types';
import {ApiVersion, ShopifyHeader} from '../../base_types';
import {Context} from '../../context';
import {DataType} from '../../clients/types';
import {assertHttpRequest} from '../../clients/http_client/test/test_helper';
Expand Down
61 changes: 61 additions & 0 deletions src/webhooks/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {StatusCode} from '@shopify/network';

import {ApiVersion} from '../base_types';

export enum DeliveryMethod {
Http = 'http',
EventBridge = 'eventbridge',
}

type WebhookHandlerFunction = (
topic: string,
shop_domain: string,
body: Buffer
) => void;

export interface RegisterOptions {
// See https://shopify.dev/docs/admin-api/graphql/reference/events/webhooksubscriptiontopic for available topics
topic: string;
path: string;
shop: string;
accessToken: string;
apiVersion: ApiVersion;
deliveryMethod?: DeliveryMethod;
webhookHandler: WebhookHandlerFunction;
}

export interface RegisterReturn {
success: boolean;
result: unknown;
}

export interface WebhookRegistryEntry {
path: string;
topic: string;
webhookHandler: WebhookHandlerFunction;
}

export interface ProcessOptions {
headers: Record<string, string>;
body: Buffer;
}

export interface ProcessReturn {
statusCode: StatusCode;
headers: Record<string, string>;
}

interface WebhookCheckResponseNode {
node: {
id: string;
callbackUrl: string;
};
}

export interface WebhookCheckResponse {
data: {
webhookSubscriptions: {
edges: WebhookCheckResponseNode[];
};
};
}

0 comments on commit 0cdbccf

Please # to comment.