utapi

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2024 License: Unlicense Imports: 9 Imported by: 0

README

utapi-go

A thin wrapper for the uploadthing api.

If you'd like to add something you need, feel free to contribute in line with contributing.md.

why?

You have uploaded a large file to uploadthing and you'd like to process that big boy in go.

setup

You will need a .env file with your uploadthing secret key.

UPLOADTHING_SECRET=sk_*************************

usage

After adding your import statement as below, run go mod tidy.

package main

import (
    "github.com/jesses-code-adventures/utapi-go"
    "os"
    "fmt"
)

func main() {
    // Create api handler
    utApi, err := utapi.NewUtApi()
    if err != nil {
        fmt.Println("Error creating uploadthing api handler")
        fmt.Println(fmt.Errorf("%s", err))
        os.Exit(1)
    }

    // Example - deleting a file
    // This is the key returned by uploadthing when you create a file
    keys := []string{"fc8d296b-20f6-4173-bfa5-5d6c32fc9f6b-geat9r.csv"}
	resp, err := utApi.DeleteFiles(keys)
	if err != nil {
		fmt.Println("Error deleting files")
		fmt.Println(fmt.Errorf("%s", err))
	} else {
		fmt.Println("Successfully deleted file")
		fmt.Println(resp.Success)
	}
}

Documentation

Index

Constants

View Source
const (
	DeletionPending uploadthingFileStatus = iota
	Failed
	Uploaded
	Uploading
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DeleteFileResponse

type DeleteFileResponse struct {
	Success bool `json:"success"`
}

Full response object for a delete file action

type ListFilesOpts

type ListFilesOpts struct {
	Limit  int `json:"limit"`
	Offset int `json:"offset"`
}

Arguments for the list files endpoint

type PresignedUrlOpts

type PresignedUrlOpts struct {
	FileKey   string `json:"fileKey"`
	ExpiresIn int    `json:"expiresIn"`
}

Arguments for the presigned url endpoint

type PresignedUrlResponse

type PresignedUrlResponse struct {
	Url string `json:"url"`
}

Represents a full response struct for a presigned url

type RenameFilesOpts

type RenameFilesOpts struct {
	Updates []SingleFileRename `json:"updates"`
}

Arguments for the rename files endpoint

type SingleFileRename

type SingleFileRename struct {
	FileKey string `json:"fileKey"`
	NewName string `json:"newName"`
}

type UploadthingFileResponse

type UploadthingFileResponse struct {
	Files []uploadthingFile `json:"files"`
}

Represents a full response struct for a list of files

type UploadthingUrl

type UploadthingUrl struct {
	Url string `json:"url"`
	Key string `json:"key"`
}

Represents a single uploadthing url

type UploadthingUrlsResponse

type UploadthingUrlsResponse struct {
	Data []UploadthingUrl `json:"data"`
}

Represents a full response struct for a list of urls

type UploadthingUsageInfo

type UploadthingUsageInfo struct {
	TotalBytes       int     `json:"totalBytes"`
	TotalReadable    string  `json:"totalReadable"`
	AppTotalBytes    float32 `json:"appTotalBytes"`
	AppTotalReadable string  `json:"appTotalReadable"`
	FilesUploaded    int     `json:"filesUploaded"`
	LimitBytes       float32 `json:"limitBytes"`
	LimitReadable    string  `json:"limitReadable"`
}

Represents a full response struct for uploadthing usage info

func (*UploadthingUsageInfo) AsString

func (u *UploadthingUsageInfo) AsString() string

type UtApi

type UtApi struct {
	// contains filtered or unexported fields
}

UtApi - Interact with the uploadthing api. This struct is designed to replicate UTApi from the uploadthing typescript sdk. Please note that responses are encoded into structs that mirror the current json. Any errors are returned as-is.

func NewUtApi

func NewUtApi() (*UtApi, error)

Construct an instance of the UtApi struct. This will read the UPLOADTHING_SECRET environment variable from the .env file. If you don't have UPLOADTHING_SECRET set, the function will throw.

func (*UtApi) DeleteFiles

func (ut *UtApi) DeleteFiles(fileKeys []string) (*DeleteFileResponse, error)

Delete files from uploadthing.

func (*UtApi) GetFileUrls

func (ut *UtApi) GetFileUrls(fileKeys []string) (*UploadthingUrlsResponse, error)

Given an array of file keys, get the corresponding urls.

func (*UtApi) GetPresignedUrl

func (ut *UtApi) GetPresignedUrl(opts PresignedUrlOpts) (string, error)

Generate a presigned url for a file. expiresIn should be a duration in seconds. The maximum value for expiresIn is 604800 (7 days). You must accept overrides on the UploadThing dashboard for expiresIn to be accepted.

func (*UtApi) GetUsageInfo

func (ut *UtApi) GetUsageInfo() (*UploadthingUsageInfo, error)

Get usage info for the current uploadthing account.

func (*UtApi) ListFiles

func (ut *UtApi) ListFiles(opts ListFilesOpts) (*UploadthingFileResponse, error)

List files stored in uploadthing.

func (*UtApi) RenameFiles

func (ut *UtApi) RenameFiles(files RenameFilesOpts) error

Rename files in uploadthing. No response is returned, but an error is returned if the request fails. This is in line with the behaviour of the uploadthing typescript sdk.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL