I'm writing a blog and want to make sure that people I'm sharing with have the correct way to initiate a Appwrite Function with TS.
This is the index.ts
import * as sdk from 'node-appwrite';
/*
'req' variable has:
'headers' - object with request headers
'payload' - request body data as a string
'variables' - object with function variables
'res' variable has:
'send(text, status)' - function to return text response. Status code defaults to 200
'json(obj, status)' - function to return JSON response. Status code defaults to 200
If an error is thrown, a response with code 500 will be returned.
*/
module.exports = async function (req: any, res: any) {
const client = new sdk.Client();
try {
if (
!req.variables['APPWRITE_FUNCTION_ENDPOINT'] &&
!req.variables['APPWRITE_FUNCTION_API_KEY']
) {
console.log(
'Environment variables are not set. Function cannot use Appwrite SDK.'
);
} else {
console.log("We've the credentials let's load client");
client
.setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
.setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
.setKey(req.variables['APPWRITE_FUNCTION_API_KEY'])
.setSelfSigned(true);
}
res.json({
areDevelopersAwesome: true,
});
} catch (error: any) {
console.log(error.toSting());
res.json({
areDevelopersAwesome: false,
});
}
};
package.json
"build" : 'tsc src/index.ts --outDir build'
appwrite.json
"entryPoint": "build/index.js"
Yep,
That looks about right,
Just make sure to run npm run build before deploying. as Appwrite won't build it for you - for now anyhow.
Hey @Nimit Savant I have just created a gist with my method to use functions with typscript: https://gist.github.com/gepd/9179e3f058180e737e046810e84f1023
yes yess :)
can you like elaborate more on this, what is this trying to do?
It compiles the files, fix paths (in my case I share functions in multiples files and multiples folders) and give you the ability to deploy to appwrite with one command, you don't to remember to compile before deploy. You can remove any postbuild file, and update to your needs.
@Nimit Savant
and how is this different from tsc?
I have updated the description in the gist to understand what is different
and how can it help you
Recommended threads
- Helping in unblock my account
I deleted my Appwrite Cloud account that was linked via GitHub. Now I activated my GitHub Student Pack and want to sign up again using the same GitHub account, ...
- Not allowed permission to upsert a prese...
```js const presenceID = ID.unique(); setPID(presenceID); const presence = await presences.upsert({ presenceId: presenceID, status: "online"...
- Request for temporary 3 to 4 hours datab...
Hi Appwrite Team, I hope you are doing well.We are an early-stage startup currently running on Appwrite Cloud. We have unfortunately exhausted our database rea...