Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | 2x 2x 2x 2x 2x 2x 2x 12x 12x 12x 12x 12x 12x 18x 6x 1x 5x 6x 1x 5x 10x 2x 8x 1x 7x 3x 3x 3x 3x 1x | import * as ShopifyErrors from '../error';
import {Session} from '../auth/session';
import {GraphqlClient} from '../clients/graphql';
import {RestClient} from '../clients/rest';
import {Context} from '../context';
import {WithSessionParams, WithSessionResponse} from './types';
import loadOfflineSession from './load-offline-session';
import loadCurrentSession from './load-current-session';
export default async function withSession({
clientType,
isOnline,
req,
res,
shop,
}: WithSessionParams): Promise<WithSessionResponse> {
Context.throwIfUninitialized();
let session: Session | undefined;
if (isOnline) {
if (!req || !res) {
throw new ShopifyErrors.MissingRequiredArgument('Please pass in both the "request" and "response" objects.');
}
session = await loadCurrentSession(req, res);
} else {
if (!shop) {
throw new ShopifyErrors.MissingRequiredArgument('Please pass in a value for "shop"');
}
session = await loadOfflineSession(shop);
}
if (!session) {
throw new ShopifyErrors.SessionNotFound('No session found.');
} else if (!session.accessToken) {
throw new ShopifyErrors.InvalidSession('Requested session does not contain an accessToken.');
}
let client: RestClient | GraphqlClient;
switch (clientType) {
case 'rest':
client = new RestClient(session.shop, session.accessToken);
return {
client,
session,
};
case 'graphql':
client = new GraphqlClient(session.shop, session.accessToken);
return {
client,
session,
};
default:
throw new ShopifyErrors.UnsupportedClientType(
`"${clientType}" is an unsupported clientType. Please use "rest" or "graphql".`,
);
}
}
|