fix sdk api
This commit is contained in:
@@ -1,83 +1,40 @@
|
||||
import type {
|
||||
BatchPayload,
|
||||
BatchUpdateProfilePayload,
|
||||
BatchUpdateSessionPayload,
|
||||
MixanErrorResponse,
|
||||
} from '@mixan/types';
|
||||
import type { PostEventPayload } from '@mixan/types';
|
||||
|
||||
type MixanLogger = (...args: unknown[]) => void;
|
||||
|
||||
// -- 1. Besök
|
||||
// -- 2. Finns profile id?
|
||||
// -- NEJ
|
||||
// -- a. skicka events som vanligt (retunera genererat ID)
|
||||
// -- b. ge möjlighet att spara
|
||||
// -- JA
|
||||
// -- a. skicka event med profile_id
|
||||
// -- Payload
|
||||
// -- - user_agent?
|
||||
// -- - ip?
|
||||
// -- - profile_id?
|
||||
// -- - referrer
|
||||
|
||||
export interface NewMixanOptions {
|
||||
export interface MixanOptions {
|
||||
url: string;
|
||||
clientId: string;
|
||||
clientSecret?: string;
|
||||
verbose?: boolean;
|
||||
setItem?: (key: string, profileId: string) => void;
|
||||
getItem?: (key: string) => string | null;
|
||||
removeItem?: (key: string) => void;
|
||||
setProfileId?: (profileId: string) => void;
|
||||
getProfileId?: () => string | null | undefined;
|
||||
removeProfileId?: () => void;
|
||||
}
|
||||
|
||||
export type MixanOptions = Required<NewMixanOptions>;
|
||||
|
||||
export interface MixanState {
|
||||
profileId: null | string;
|
||||
profileId?: string;
|
||||
properties: Record<string, unknown>;
|
||||
}
|
||||
|
||||
function createLogger(verbose: boolean): MixanLogger {
|
||||
return verbose ? (...args) => console.log('[Mixan]', ...args) : () => {};
|
||||
}
|
||||
|
||||
class Fetcher {
|
||||
private url: string;
|
||||
private clientId: string;
|
||||
private clientSecret: string;
|
||||
|
||||
constructor(
|
||||
options: MixanOptions,
|
||||
private logger: MixanLogger
|
||||
) {
|
||||
this.url = options.url;
|
||||
this.clientId = options.clientId;
|
||||
this.clientSecret = options.clientSecret;
|
||||
}
|
||||
|
||||
post<PostData, PostResponse>(
|
||||
function createApi(_url: string, clientId: string, clientSecret?: string) {
|
||||
return function post<ReqBody, ResBody>(
|
||||
path: string,
|
||||
data?: PostData,
|
||||
data: ReqBody,
|
||||
options?: RequestInit
|
||||
): Promise<PostResponse | null> {
|
||||
const url = `${this.url}${path}`;
|
||||
): Promise<ResBody | null> {
|
||||
const url = `${_url}${path}`;
|
||||
let timer: ReturnType<typeof setTimeout>;
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
'mixan-client-id': clientId,
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
if (clientSecret) {
|
||||
headers['mixan-client-secret'] = clientSecret;
|
||||
}
|
||||
return new Promise((resolve) => {
|
||||
const wrappedFetch = (attempt: number) => {
|
||||
clearTimeout(timer);
|
||||
|
||||
this.logger(
|
||||
`Request attempt ${attempt + 1}: ${url}`,
|
||||
JSON.stringify(data, null, 2)
|
||||
);
|
||||
|
||||
fetch(url, {
|
||||
headers: {
|
||||
['mixan-client-id']: this.clientId,
|
||||
['mixan-client-secret']: this.clientSecret,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
headers,
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data ?? {}),
|
||||
keepalive: true,
|
||||
@@ -88,15 +45,13 @@ class Fetcher {
|
||||
return retry(attempt, resolve);
|
||||
}
|
||||
|
||||
const response = (await res.json()) as
|
||||
| MixanErrorResponse
|
||||
| PostResponse;
|
||||
const response = await res.json();
|
||||
|
||||
if (!response) {
|
||||
return resolve(null);
|
||||
}
|
||||
|
||||
resolve(response as PostResponse);
|
||||
resolve(response as ResBody);
|
||||
})
|
||||
.catch(() => {
|
||||
return retry(attempt, resolve);
|
||||
@@ -105,9 +60,9 @@ class Fetcher {
|
||||
|
||||
function retry(
|
||||
attempt: number,
|
||||
resolve: (value: PostResponse | null) => void
|
||||
resolve: (value: ResBody | null) => void
|
||||
) {
|
||||
if (attempt > 3) {
|
||||
if (attempt > 1) {
|
||||
return resolve(null);
|
||||
}
|
||||
|
||||
@@ -121,88 +76,85 @@ class Fetcher {
|
||||
|
||||
wrappedFetch(0);
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export class Mixan {
|
||||
private options: MixanOptions;
|
||||
private fetch: Fetcher;
|
||||
private logger: (...args: any[]) => void;
|
||||
export class Mixan<Options extends MixanOptions = MixanOptions> {
|
||||
public options: Options;
|
||||
private api: ReturnType<typeof createApi>;
|
||||
private state: MixanState = {
|
||||
profileId: null,
|
||||
properties: {},
|
||||
};
|
||||
|
||||
constructor(options: NewMixanOptions) {
|
||||
this.logger = createLogger(options.verbose ?? false);
|
||||
this.options = {
|
||||
verbose: false,
|
||||
clientSecret: '',
|
||||
...options,
|
||||
};
|
||||
this.fetch = new Fetcher(this.options, this.logger);
|
||||
constructor(options: Options) {
|
||||
this.options = options;
|
||||
this.api = createApi(options.url, options.clientId, options.clientSecret);
|
||||
}
|
||||
|
||||
// Public
|
||||
|
||||
public init(properties?: Record<string, unknown>) {
|
||||
this.logger('Init');
|
||||
this.state.properties = properties ?? {};
|
||||
}
|
||||
|
||||
public setUser(payload: Omit<BatchUpdateProfilePayload, 'profileId'>) {
|
||||
this.batcher.add({
|
||||
type: 'update_profile',
|
||||
payload: {
|
||||
...payload,
|
||||
properties: payload.properties ?? {},
|
||||
profileId: this.state.profileId,
|
||||
},
|
||||
});
|
||||
// public setUser(payload: Omit<BatchUpdateProfilePayload, 'profileId'>) {
|
||||
// this.batcher.add({
|
||||
// type: 'update_profile',
|
||||
// payload: {
|
||||
// ...payload,
|
||||
// properties: payload.properties ?? {},
|
||||
// profileId: this.state.profileId,
|
||||
// },
|
||||
// });
|
||||
// }
|
||||
|
||||
// public increment(name: string, value: number) {
|
||||
// this.batcher.add({
|
||||
// type: 'increment',
|
||||
// payload: {
|
||||
// name,
|
||||
// value,
|
||||
// profileId: this.state.profileId,
|
||||
// },
|
||||
// });
|
||||
// }
|
||||
|
||||
// public decrement(name: string, value: number) {
|
||||
// this.batcher.add({
|
||||
// type: 'decrement',
|
||||
// payload: {
|
||||
// name,
|
||||
// value,
|
||||
// profileId: this.state.profileId,
|
||||
// },
|
||||
// });
|
||||
// }
|
||||
|
||||
private getProfileId() {
|
||||
if (this.state.profileId) {
|
||||
return this.state.profileId;
|
||||
} else if (this.options.getProfileId) {
|
||||
this.state.profileId = this.options.getProfileId() || undefined;
|
||||
}
|
||||
}
|
||||
|
||||
public increment(name: string, value: number) {
|
||||
this.batcher.add({
|
||||
type: 'increment',
|
||||
payload: {
|
||||
name,
|
||||
value,
|
||||
profileId: this.state.profileId,
|
||||
public async event(name: string, properties?: Record<string, unknown>) {
|
||||
const profileId = await this.api<PostEventPayload, string>('/event', {
|
||||
name,
|
||||
properties: {
|
||||
...this.state.properties,
|
||||
...(properties ?? {}),
|
||||
},
|
||||
timestamp: this.timestamp(),
|
||||
profileId: this.getProfileId(),
|
||||
});
|
||||
}
|
||||
|
||||
public decrement(name: string, value: number) {
|
||||
this.batcher.add({
|
||||
type: 'decrement',
|
||||
payload: {
|
||||
name,
|
||||
value,
|
||||
profileId: this.state.profileId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
public event(name: string, properties?: Record<string, unknown>) {
|
||||
this.fetch
|
||||
.post('/event', {
|
||||
name,
|
||||
properties: {
|
||||
...this.state.properties,
|
||||
...(properties ?? {}),
|
||||
},
|
||||
time: this.timestamp(),
|
||||
profileId: this.state.profileId,
|
||||
})
|
||||
.then((response) => {
|
||||
if ('profileId' in response) {
|
||||
this.options.setItem('@mixan:profileId', response.profileId);
|
||||
}
|
||||
});
|
||||
if (this.options.setProfileId && profileId) {
|
||||
this.options.setProfileId(profileId);
|
||||
}
|
||||
}
|
||||
|
||||
public setGlobalProperties(properties: Record<string, unknown>) {
|
||||
this.logger('Set global properties', properties);
|
||||
this.state.properties = {
|
||||
...this.state.properties,
|
||||
...properties,
|
||||
@@ -210,9 +162,10 @@ export class Mixan {
|
||||
}
|
||||
|
||||
public clear() {
|
||||
this.logger('Clear / Logout');
|
||||
this.options.removeItem('@mixan:profileId');
|
||||
this.state.profileId = null;
|
||||
this.state.profileId = undefined;
|
||||
if (this.options.removeProfileId) {
|
||||
this.options.removeProfileId();
|
||||
}
|
||||
}
|
||||
|
||||
public setUserProperty(name: string, value: unknown, update = true) {
|
||||
|
||||
Reference in New Issue
Block a user