Skip to content

Commit

Permalink
feat: adding twelvelabs api call test
Browse files Browse the repository at this point in the history
  • Loading branch information
ca513zn committed Sep 4, 2024
1 parent 0310f8d commit 8b71380
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 8 deletions.
4 changes: 3 additions & 1 deletion amplify/functions/api-function/handler.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import type { APIGatewayProxyHandler } from "aws-lambda";
import { TwelveLabs } from "twelvelabs-js";

export const handler: APIGatewayProxyHandler = async (event) => {
console.log("event", event);
const client = new TwelveLabs({ apiKey: "<YOUR_API_KEY>" });
return {
statusCode: 200,
// Modify the CORS settings below to match your specific requirements
headers: {
"Access-Control-Allow-Origin": "*", // Restrict this to domains you trust
"Access-Control-Allow-Headers": "*", // Specify only the headers you need to allow
},
body: JSON.stringify("Hello from myFunction!"),
body: JSON.stringify(client),
};
};
102 changes: 96 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"@aws-amplify/ui-react": "^6.2.0",
"aws-amplify": "^6.5.4",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"twelvelabs-js": "^0.2.6"
},
"devDependencies": {
"@aws-amplify/backend": "^1.1.1",
Expand Down
2 changes: 2 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect, useState } from "react";
import type { Schema } from "../amplify/data/resource";
import { generateClient } from "aws-amplify/data";
import { getItem } from "./services/api";

const client = generateClient<Schema>();

Expand Down Expand Up @@ -38,6 +39,7 @@ function App() {
<a href="https://docs.amplify.aws/react/start/quickstart/#make-frontend-updates">
Review next step of this tutorial.
</a>
<button onClick={() => getItem()}>Test</button>
</div>
</main>
);
Expand Down
8 changes: 8 additions & 0 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import { Amplify } from "aws-amplify";
import outputs from "../amplify_outputs.json";

Amplify.configure(outputs);
const existingConfig = Amplify.getConfig();
Amplify.configure({
...existingConfig,
API: {
...existingConfig.API,
REST: outputs.custom.API,
},
});

ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
Expand Down
16 changes: 16 additions & 0 deletions src/services/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { get } from "aws-amplify/api";

export async function getItem() {
try {
const restOperation = get({
apiName: "myRestApi",
path: "items",
});
const { body } = await restOperation.response;
// consume as a string:
const str = await body.text();
console.log("GET call succeeded: ", str);
} catch (error) {
console.log("GET call failed: ", JSON.parse(error as string));
}
}

0 comments on commit 8b71380

Please sign in to comment.