@slicemachine/init 1.0.2-alpha.4 → 1.0.2-alpha.44

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,76 @@
1
+ import axios from "axios";
2
+ import * as t from "io-ts";
3
+ import { pipe } from "fp-ts/function";
4
+ import { fold } from "fp-ts/Either";
5
+ import { Utils, Communication, FileSystem } from "@slicemachine/core";
6
+
7
+ const UserProfile = t.exact(
8
+ t.type({
9
+ userId: t.string,
10
+ shortId: t.string,
11
+ email: t.string,
12
+ firstName: t.string,
13
+ lastName: t.string,
14
+ })
15
+ );
16
+
17
+ export type UserProfile = t.TypeOf<typeof UserProfile>;
18
+
19
+ export async function getUserProfile(
20
+ cookies: string,
21
+ base = Utils.CONSTS.DEFAULT_BASE
22
+ ): Promise<UserProfile> {
23
+ // note the auth server also provides a userId
24
+
25
+ const url = new URL(base);
26
+ url.hostname = `user.${url.hostname}`;
27
+ url.pathname = "profile";
28
+
29
+ const endpoint = url.toString();
30
+ const token = Utils.Cookie.parsePrismicAuthToken(cookies);
31
+
32
+ return axios
33
+ .get<UserProfile>(endpoint, {
34
+ headers: {
35
+ Authorization: `Bearer Token ${token}`,
36
+ },
37
+ })
38
+ .then((res) =>
39
+ pipe(
40
+ UserProfile.decode(res.data),
41
+ fold<t.Errors, UserProfile, UserProfile>(
42
+ () => {
43
+ throw new Error("Can't parse user profile");
44
+ },
45
+ (data: UserProfile) => data
46
+ )
47
+ )
48
+ );
49
+ }
50
+
51
+ export async function validateSessionAndGetProfile(
52
+ base = Utils.CONSTS.DEFAULT_BASE
53
+ ): Promise<{
54
+ info: Communication.UserInfo;
55
+ profile: UserProfile | null;
56
+ } | null> {
57
+ const config = FileSystem.PrismicSharedConfigManager.get();
58
+
59
+ if (!config.cookies.length) return Promise.resolve(null); // default config, logged out.
60
+ if (base != config.base) return Promise.resolve(null); // not the same base so it doesn't
61
+
62
+ try {
63
+ const info = await Communication.validateSession(config.cookies, base);
64
+ const profile = await getUserProfile(config.cookies, base).catch(
65
+ () => null
66
+ );
67
+ if (profile?.shortId) {
68
+ FileSystem.PrismicSharedConfigManager.setProperties({
69
+ shortId: profile.shortId,
70
+ });
71
+ }
72
+ return { info, profile };
73
+ } catch {
74
+ return null;
75
+ }
76
+ }
@@ -1,15 +1,15 @@
1
1
  import util from "util";
2
2
  import { exec } from "child_process";
3
3
 
4
- export function findArgument(args: string[], name: string): string | null {
4
+ export function findArgument(args: string[], name: string): string | undefined {
5
5
  const flagIndex: number = args.indexOf(`--${name}`);
6
6
 
7
- if (flagIndex === -1) return null;
8
- if (args.length < flagIndex + 2) return null;
7
+ if (flagIndex === -1) return;
8
+ if (args.length < flagIndex + 2) return;
9
9
 
10
10
  const flagValue = args[flagIndex + 1];
11
11
 
12
- if (flagValue.startsWith("--")) return null;
12
+ if (flagValue.startsWith("--")) return;
13
13
  return flagValue;
14
14
  }
15
15
 
@@ -0,0 +1,57 @@
1
+ /*
2
+ * DUPLICATION
3
+ * this file is a duplication of ServerTracker in sm-ui
4
+ * this duplication has been done on purpose to simplify the merge if init becomes part of sm-ui
5
+ * or if we decide to share the tracker implementation from a lib, but it'll need some refactoring
6
+ * Duplication means no entanglement so don't merge this with the other implem and keep the structure.
7
+ */
8
+
9
+ import ServerAnalytics from "analytics-node";
10
+
11
+ export enum EventType {
12
+ DownloadLibrary = "SliceMachine Download Library",
13
+ }
14
+
15
+ export class Tracker {
16
+ constructor(
17
+ readonly analytics: ServerAnalytics,
18
+ readonly repo: string,
19
+ readonly identifier: { userId: string } | { anonymousId: string },
20
+ readonly tracking = true
21
+ ) {}
22
+
23
+ static build(
24
+ writeKey: string,
25
+ repo: string | undefined,
26
+ identifier: { userId: string } | { anonymousId: string },
27
+ tracking = true
28
+ ): Tracker | undefined {
29
+ try {
30
+ if (!repo) return;
31
+ const analytics = new ServerAnalytics(writeKey);
32
+ return new Tracker(analytics, repo, identifier, tracking);
33
+ } catch (error) {
34
+ console.warn(error);
35
+ return;
36
+ }
37
+ }
38
+
39
+ private trackEvent(
40
+ eventType: EventType,
41
+ attributes: Record<string, unknown> = {}
42
+ ): void {
43
+ this.tracking &&
44
+ this.analytics.track({
45
+ event: eventType,
46
+ ...this.identifier,
47
+ properties: attributes,
48
+ });
49
+ }
50
+
51
+ Track = {
52
+ // not called, for demo only
53
+ downloadLibrary: (library: string): void => {
54
+ this.trackEvent(EventType.DownloadLibrary, { library });
55
+ },
56
+ };
57
+ }