@slicemachine/init 1.1.10-alpha.3 → 1.1.10

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.
@@ -1,171 +0,0 @@
1
- import { CustomType } from "@prismicio/types-internal/lib/customtypes";
2
- import { Slices, SliceSM } from "@slicemachine/core/build/models";
3
- import axios from "axios";
4
- import { logs } from "../../utils";
5
- import type { ApiEndpoints } from "./endpoints";
6
-
7
- export function handleErrors(prefix: string, err: unknown) {
8
- if (axios.isAxiosError(err) && err.response) {
9
- logs.writeError(
10
- `${prefix} | [${err.response.status}]: ${err.response.statusText}`
11
- );
12
- } else if (err instanceof Error) {
13
- logs.writeError(`${prefix} ${err.message}`);
14
- } else {
15
- logs.writeError(`${prefix} ${String(err)}`);
16
- }
17
- }
18
-
19
- export async function getRemoteSliceIds(
20
- customTypeApiEndpoint: ApiEndpoints["Models"],
21
- repository: string,
22
- authorization: string
23
- ): Promise<Array<string>> {
24
- const addr = `${stripLastSlash(customTypeApiEndpoint)}/slices`;
25
- return axios
26
- .get<Array<{ id: string }>>(addr, {
27
- headers: {
28
- Authorization: `Bearer ${authorization}`,
29
- repository,
30
- },
31
- })
32
- .then((res) => {
33
- return Array.isArray(res.data) ? res.data.map((model) => model.id) : [];
34
- });
35
- }
36
-
37
- async function sendModelToPrismic(
38
- repository: string,
39
- authorization: string,
40
- customTypesApiEndpoint: string,
41
- remoteSliceIds: Array<string>,
42
- model: SliceSM
43
- ): Promise<void> {
44
- const data = Slices.fromSM(model);
45
- const updateOrInsertUrl = `${customTypesApiEndpoint}slices/${
46
- remoteSliceIds.includes(model.id) ? "update" : "insert"
47
- }`;
48
-
49
- return axios
50
- .post(updateOrInsertUrl, data, {
51
- headers: {
52
- Authorization: `Bearer ${authorization}`,
53
- repository,
54
- },
55
- })
56
- .then(() => {
57
- return;
58
- })
59
- .catch((err) => {
60
- handleErrors(
61
- `sending slice ${model.id}, please try again. If the problem persists, contact us.`,
62
- err
63
- );
64
- throw err;
65
- });
66
- }
67
-
68
- export async function sendManyModelsToPrismic(
69
- repository: string,
70
- authorization: string,
71
- customTypesApiEndpoint: string,
72
- remoteSliceIds: Array<string>,
73
- models: Array<SliceSM>
74
- ): Promise<void> {
75
- return Promise.all(
76
- models.map((model) =>
77
- sendModelToPrismic(
78
- repository,
79
- authorization,
80
- customTypesApiEndpoint,
81
- remoteSliceIds,
82
- model
83
- )
84
- )
85
- )
86
- .then(() => {
87
- return;
88
- })
89
- .catch(() => {
90
- process.exit(1);
91
- });
92
- }
93
-
94
- function stripLastSlash(str: string) {
95
- return str.replace(/\/*$/g, "");
96
- }
97
-
98
- export function getRemoteCustomTypeIds(
99
- customTypeApiEndpoint: ApiEndpoints["Models"],
100
- repository: string,
101
- authorization: string
102
- ): Promise<Array<string>> {
103
- const addr = `${stripLastSlash(customTypeApiEndpoint)}/customtypes`;
104
- return axios
105
- .get<CustomType[]>(addr, {
106
- headers: {
107
- Authorization: `Bearer ${authorization}`,
108
- repository,
109
- },
110
- })
111
- .then((res) => {
112
- return Array.isArray(res.data) ? res.data.map((ct) => ct.id) : [];
113
- });
114
- }
115
-
116
- async function sendCustomTypeToPrismic(
117
- repository: string,
118
- authorization: string,
119
- customTypeApiEndpoint: string,
120
- remoteCustomTypeIds: Array<string>,
121
- customType: CustomType
122
- ): Promise<void> {
123
- const shouldUpdate = remoteCustomTypeIds.includes(customType.id);
124
- const addr = `${stripLastSlash(customTypeApiEndpoint)}/customtypes/${
125
- shouldUpdate ? "update" : "insert"
126
- }`;
127
-
128
- return axios
129
- .post(addr, customType, {
130
- headers: {
131
- repository,
132
- Authorization: `Bearer ${authorization}`,
133
- },
134
- })
135
- .then(() => {
136
- return;
137
- })
138
- .catch((err) => {
139
- handleErrors(
140
- `sending custom type ${customType.id}, please try again. If the problem persists, contact us.`,
141
- err
142
- );
143
- throw err;
144
- });
145
- }
146
-
147
- export async function sendManyCustomTypesToPrismic(
148
- repository: string,
149
- authorization: string,
150
- customTypeApiEndpoint: string,
151
- remoteCustomTypeIds: Array<string>,
152
- customTypes: Array<CustomType>
153
- ): Promise<void> {
154
- return Promise.all(
155
- customTypes.map((customType) =>
156
- sendCustomTypeToPrismic(
157
- repository,
158
- authorization,
159
- customTypeApiEndpoint,
160
- remoteCustomTypeIds,
161
- customType
162
- )
163
- )
164
- )
165
- .then(() => {
166
- return;
167
- })
168
- .catch(() => {
169
- process.exit(1);
170
- });
171
- }
@@ -1,20 +0,0 @@
1
- export type ApiEndpoints = {
2
- Models: string;
3
- AclProvider: string;
4
- };
5
-
6
- const ProductionApisEndpoints: ApiEndpoints = {
7
- Models: "https://customtypes.prismic.io/",
8
- AclProvider: "https://0yyeb2g040.execute-api.us-east-1.amazonaws.com/prod/",
9
- };
10
-
11
- const StageApisEndpoints: ApiEndpoints = {
12
- Models: "https://customtypes.wroom.io/",
13
- AclProvider: "https://2iamcvnxf4.execute-api.us-east-1.amazonaws.com/stage/",
14
- };
15
-
16
- export const getEndpointsFromBase = (base: string): ApiEndpoints => {
17
- const url = new URL(base);
18
- if (url.hostname === "wroom.io") return StageApisEndpoints;
19
- return ProductionApisEndpoints;
20
- };
@@ -1,72 +0,0 @@
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, Models, CONSTS } from "@slicemachine/core";
6
- import * as Prismic from "@slicemachine/core/build/prismic";
7
-
8
- export async function getUserProfile(
9
- cookies: string,
10
- base = CONSTS.DEFAULT_BASE
11
- ): Promise<Models.UserProfile> {
12
- const userServiceBase =
13
- CONSTS.DEFAULT_BASE === base
14
- ? CONSTS.USER_SERVICE_BASE
15
- : CONSTS.USER_SERVICE_STAGING_BASE;
16
-
17
- // note the auth server also provides a userId
18
- const url = new URL(userServiceBase);
19
- url.pathname = "profile";
20
-
21
- const endpoint = url.toString();
22
- const token = Utils.Cookie.parsePrismicAuthToken(cookies);
23
-
24
- return axios
25
- .get<Models.UserProfile>(endpoint, {
26
- headers: {
27
- Authorization: `Bearer ${token}`,
28
- },
29
- })
30
- .then((res) =>
31
- pipe(
32
- Models.UserProfile.decode(res.data),
33
- fold<t.Errors, Models.UserProfile, Models.UserProfile>(
34
- () => {
35
- throw new Error("Can't parse user profile");
36
- },
37
- (data: Models.UserProfile) => data
38
- )
39
- )
40
- );
41
- }
42
-
43
- export async function validateSessionAndGetProfile(
44
- base = CONSTS.DEFAULT_BASE
45
- ): Promise<{
46
- info: Models.UserInfo;
47
- profile: Models.UserProfile | null;
48
- } | null> {
49
- const config = Prismic.PrismicSharedConfigManager.get();
50
-
51
- if (!config.cookies.length) return Promise.resolve(null); // default config, logged out.
52
- if (base != config.base) return Promise.resolve(null); // not the same base so it doesn't
53
-
54
- try {
55
- const info = await Prismic.Communication.validateSession(
56
- config.cookies,
57
- base
58
- );
59
- const profile = await getUserProfile(config.cookies, base).catch(
60
- () => null
61
- );
62
- if (profile?.shortId) {
63
- Prismic.PrismicSharedConfigManager.setProperties({
64
- shortId: profile.shortId,
65
- intercomHash: profile.intercomHash,
66
- });
67
- }
68
- return { info, profile };
69
- } catch {
70
- return null;
71
- }
72
- }