· 5 years ago · Nov 26, 2020, 07:46 PM
1import axios, { AxiosRequestConfig } from "axios";
2import { type } from "os";
3import { config } from "./config";
4
5// test input
6export interface queryStringParametersArgs {
7 url: string;
8 format: string;
9}
10
11export interface eventsArgs {
12 queryStringParameters: queryStringParametersArgs;
13}
14
15export interface OembedData {
16 version: string;
17 type: string;
18 title: string;
19 author_name: string;
20 author_url: string;
21 provider_name: string;
22 provider_url: string;
23 html: string;
24 width: number;
25 height: number;
26}
27
28let params: queryStringParametersArgs = {
29 url: "https://lottiefiles.com/39423-work-from-home-setup",
30 format: "json",
31};
32
33// oembed response spec template
34
35// check if URL parameter is valid and return regex match groups
36function parseUrl(url: string): boolean | RegExpExecArray {
37 // expression checks if matches example public url https://lottiefiles.com/39423-work-from-home-setup
38 let urlExpression: RegExp = /(?!.+https:\/\/).+(lottiefiles\.com)\/([\d]+)([-a-zA-Z]+)/g;
39 let match = urlExpression.exec(url);
40 if (match !== null) {
41 return match;
42 } else {
43 return false;
44 }
45}
46
47// get bearer token
48async function getApiToken(): Promise<any> {
49 var data = JSON.stringify({
50 query: `mutation keyLogin ($key: String!, $secret: String!, $scope: String!){
51 keyLogin (key: $key, secret: $secret, scope: $scope){
52 accessToken
53 tokenType
54 }
55 }`,
56 variables: {
57 key: config.API_KEY,
58 secret: config.API_SECRET,
59 scope: "search-animations view-animation",
60 },
61 });
62
63 let requestConfig: AxiosRequestConfig = {
64 method: "POST",
65 url: config.API_URL,
66 headers: {
67 "Content-Type": "application/json",
68 },
69 data: data,
70 };
71
72 try {
73 let response = await axios(requestConfig);
74 return response.data.data.keyLogin.accessToken;
75 } catch (error) {
76 if (error) {
77 console.log("getApiToken error: " + error.message);
78 return error.message;
79 }
80 }
81}
82
83// query graphql api
84async function getAnimationDetails(
85 animationId: any,
86 token: string
87): Promise<any> {
88 let data = JSON.stringify({
89 query: `query publicAnimation ($animationId: Float!){
90 publicAnimation (id: $animationId) {
91 name
92 lottieUrl
93 webUrl
94 publishedBy {
95 name
96 url
97 avatarUrl
98 }
99 }
100 }`,
101 variables: { animationId: animationId },
102 });
103
104 let requestConfig: AxiosRequestConfig = {
105 method: "POST",
106 url: config.API_URL,
107 headers: {
108 Authorization: `Bearer ${token}`,
109 "Content-Type": "application/json",
110 },
111 data: data,
112 };
113
114 try {
115 let response = await axios(requestConfig);
116 return response.data.data;
117 } catch (error) {
118 if (error) {
119 console.log("getAnimationDetails error: " + error.message);
120 return error.message;
121 }
122 }
123}
124
125// TODO: prepare oEmbed response
126
127async function defaultHandler(events: eventsArgs) {
128 // let defaultResponseFormat = "json";
129
130 let response = {};
131 const validated = false;
132
133 const { url, format } = events.queryStringParameters;
134
135 if (typeof url === "undefined" || typeof url !== "string") {
136 responseBody = "The url value must be a string.";
137 }
138
139 if (format && format !== "json") {
140 responseBody = "The requested format has not been implemented.";
141 }
142
143 if (valid) {
144 try {
145 } catch (error) {
146 response = {
147 statusCode: 500,
148 body: JSON.stringify(error),
149 };
150 }
151 }
152
153 return response;
154
155 // 1 . both of them are defined
156
157 // 2. if params.form === json else error
158
159 // 3. check if string
160
161 const getId = parseUrl(events.queryStringParameters.url);
162
163 if (params !== null && params.url) {
164 if (params.format != "json") {
165 console.log("not implemented");
166 } else {
167 let queryParams: any = parseUrl(params.url);
168 if (queryParams) {
169 let result = await getAnimationDetails(
170 Number(queryParams[2]),
171 await getApiToken()
172 );
173 if (typeof result.publicAnimation !== "undefined") {
174 const {
175 name: title,
176 webUrl: animationData,
177 publishedBy,
178 } = result.publicAnimation;
179
180 const { name: author_name, url: author_url } = publishedBy;
181
182 const html = /*html*/ `<iframe src="https://embed.lottiefiles.com/iframe/${Number(
183 queryParams[2]
184 )}" title="${title}"></iframe>`;
185
186 const oembedResponse: OembedData = {
187 version: "1.0",
188 type: "rich",
189 title,
190 author_name,
191 author_url,
192 provider_name: "Lottiefiles",
193 provider_url: "https://lottiefiles.com",
194 html,
195 width: 300,
196 height: 300,
197 };
198
199 // console.log(result.publicAnimation);
200 // oembedResponse.title = result.publicAnimation.name;
201 // oembedResponse.author_name =
202 // result.publicAnimation.publishedBy.name;
203 // oembedResponse.author_url =
204 // result.publicAnimation.publishedBy.url;
205 // oembedResponse.html = result.publicAnimation.webUrl;
206
207 // console.log(oembedResponse);
208 responseCode = 200;
209 responseBody = oembedResponse;
210 } else {
211 console.log(result);
212 }
213 } else {
214 console.log(queryParams);
215 }
216 }
217 }
218 console.log(`Response: statusCode: ${responseCode},
219 body: ${JSON.stringify(responseBody)}`);
220
221 return {
222 statusCode: responseCode,
223 body: responseBody,
224 };
225}
226
227defaultHandler();
228