cyclops-frontend-api
API-part of the cyclops-frontend. This package contains all non-DOM changes, and interactions with CYCLOPS, from login, to payment-initiations, to user-fetching and whatnot. Below are more content and info on functions, schemas, interfaces that are being used in this package..
This package used to use Ajv for response-validation of the json schema. This package, it's validation vs unvalidated functions and tests have been removed to allow the client more controll of the response.
When fetching a user through /users/me
a cyclops_seg
cookie is set, if the user has adSegments
as a property.
Please note that this library is still in active development..
Table of Contents
Design
Interfaces can be found in ./src/**/interface.ts
.
Using the package
If you are not afraid of bloat, this package can be used with @aller/cyclops-frontend-api
.
If you are afraid of bloat, everything can be referenced with @aller/cyclops-frontend-api/lib/node|client/**
(e.g. @aller/cyclops-frontend-api/lib/user/login
). In this, you'll only include the parts of code you actually need, instead of the whole package.
For packages that require node-like environment-transpiled code, point to the lib/node
folder, while client-side code should point to lib/client
.
Functions
User
GET
async getCurrentUser(
domain: string = '',
options?: any
): Promise<IUser>
async getCannonicalUser(
domain: string = '',
options?: any
): Promise<IUser>
Subscription
async subscribe(
productId: string,
accessCode: string,
dealId: string,
options?: any,
): Promise<boolean>
DELETE
async unsubscribe(
productId: string,
domain: string = '',
options?: any
): Promise<boolean>
Payment
POST
async initiatePayment(
productId: string,
dealId: string,
options: IPaymentOptions = defaultPaymentOptions,
domain: string = '',
fetchOptions?: any
): Promise<IPaymentInitResponse>
Cyclops
GET
async cyclopsGet<T>(
path: string,
options?: any,
absoluteUrl?: boolean
): Promise<T>
POST
async cyclopsPost<T>(
path: string,
data: any,
options: any = {},
version?: string,
): Promise<T> =>
DELETE
async cyclopsDelete<T>(
path: string,
data: any,
options: any = {},
): Promise<T>
Login/logout
cyclopsLogin(
redirect?: string,
domain: string = '',
options?: any
)
async cyclopsLogout(
refresh: boolean = false,
domain: string = '',
options?: any
)
Listener
The listener-system is to allow for listening to requests being made to certain endpoints, in case you want a reactive component, be reactive on a separate component without a bunch of hacks
import { addListener, removeListener } from '@aller/cyclops-frontend-api/lib/client/cyclops/listener'
const listenerFunction = (event:CustomEvent) => {
if(!event.detail) {
return
}
DO YOUR STUFF HERE
}
addListener(ENDPOINT_URL_GOES_HERE, listenerFunction)
removeListener(ENDPOINT_URL_GOES_HERE, listenerFunction)
Catalogue
GET
async getCatalogue(
domain: string = '',
options?: any
): Promise<ICatalogue>
Constants
Cyclops
export const cyclopsLoginRelativeURL = (
redirect?: string,
domain: string = '',
options?: any,
): string => {
const red = redirect ? redirect : window.location.pathname;
const relativeUrl = `${domain}${CYCLOPS_API}/login?cyclops-redirect=${red}`;
return relativeUrl;
};
export const cyclopsLoginRelativeURLServer = (
redirect?: string,
domain: string = '',
options?: any,
): string => {
const red = redirect ? `?cyclops-redirect=${redirect}` : '';
const relativeUrl = `${domain}${CYCLOPS_API}/${red}`;
return relativeUrl;
};
Interfaces
User
interface IUser {
readonly cyclopsId: string;
readonly mediaConnectId: string;
readonly aid: string;
readonly email: string;
readonly fullName: string;
readonly phoneNumber: string;
readonly subscriptions?: ReadonlyArray<ISubscription>;
readonly newsletters?: ReadonlyArray<INewsletter>;
readonly legacySubscriptions?: ReadonlyArray<ILegacySubscription>;
readonly paymentHistory: IPaymentHistory;
readonly adSegments?: ReadonlyArray<string>;
readonly consents?: ReadonlyArray<IConsentType>;
readonly newsletterPreferences?: ReadonlyArray<INewsLetterPreference>;
readonly mobileSubscriptions?: IMobileSubscription[];
}
Subscription
interface IBaseSubscription {
readonly productId: string;
readonly status: 'ACTIVE' | 'CANCELLED';
}
interface ISubscription extends IBaseSubscription {
readonly endDate?: string;
readonly subscriptionId?: string;
readonly startDate?: string;
readonly nextChargeDate?: string;
readonly chargeAmount?: number;
readonly chargeInterval?: string;
readonly dealId?: string;
}
interface ILegacySubscription extends IBaseSubscription {}
interface IMobileSubscription extends IBaseSubscription {
readonly endDate?: string;
readonly subscriptionId?: string;
readonly startDate?: string;
readonly dealId?: string;
readonly dataGb?: string;
readonly phoneNumber?: string;
readonly signupDate?: string;
readonly chargeAmount?: number;
readonly chargeInterval?: string;
}
Newsletter
interface INewsletter {
readonly newsletterId: string;
readonly site: string;
}
Product
interface IProduct {
readonly productName: string;
readonly productDescription: string;
readonly productId: string;
readonly productLogo: string;
readonly productType: 'digital' | 'print';
readonly deals: ReadonlyArray<IProductDeal>;
}
interface IProductDeal {
readonly dealName: string;
readonly dealDescription: string;
readonly dealId: string;
readonly dealLogo: string;
readonly fullTermsDescription: string;
}
Payment
type TPaymentProvider = 'vipps' | 'nets';
interface IPaymentInitRequest {
productId: string;
dealId: string;
embeddedCheckoutUrl: string;
allowUnauthenticatedCheckout: boolean;
paymentProvider?: TPaymentProvider;
redirectURL?: string;
}
interface IPaymentInitResponse {
paymentId: string;
paymentProvider: TPaymentProvider;
reservationId?: string;
confirmationUrl?: string;
}
interface IPaymentOptions {
allowUnauthenticatedCheckout?: boolean;
embeddedCheckoutUrl?: string;
paymentProvider?: TPaymentProvider;
redirectURL?: string;
}
Catalogue
interface ICatalogue {
readonly brands: ReadonlyArray<IBrand>;
}
Brand
interface IBrand {
readonly brandName: string;
readonly brandDescription: string;
readonly brandId: string;
readonly brandLogo: string;
readonly products: ReadonlyArray<IProduct>;
}