Back

Any suggetions to my TS Appwrite function?

  • 1
  • Functions
  • Cloud
Nimit Savant
21 Jun, 2023, 21:29

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

TypeScript
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

TypeScript
"build" : 'tsc src/index.ts --outDir build' 

appwrite.json

TypeScript
"entryPoint": "build/index.js"
TL;DR
The user has created a TypeScript function for Appwrite in a GitHub gist. They explain that the function compiles files, fixes paths, and allows for easy deployment to Appwrite. They provide a link to the gist for more details. Another user mentions that running `npm run build` before deploying is necessary as Appwrite does not build it for you. The user includes the code for the `index.ts` file, `package.json`, and `appwrite.json`. Solution: If you want to use the user's TypeScript function, follow the instructions in the `index.ts` file and run `npm run build` before deploying to App
Binyamin
21 Jun, 2023, 22:43

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.

Guille
22 Jun, 2023, 15:08

Hey @Nimit Savant I have just created a gist with my method to use functions with typscript: https://gist.github.com/gepd/9179e3f058180e737e046810e84f1023

Nimit Savant
22 Jun, 2023, 17:15

yes yess :)

Nimit Savant
22 Jun, 2023, 19:40

can you like elaborate more on this, what is this trying to do?

Guille
22 Jun, 2023, 19:49

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.

Guille
22 Jun, 2023, 19:49

@Nimit Savant

Nimit Savant
22 Jun, 2023, 19:56

and how is this different from tsc?

Guille
22 Jun, 2023, 21:35

I have updated the description in the gist to understand what is different

Guille
22 Jun, 2023, 21:36

and how can it help you

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more