Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Express package / extract API_URL #87

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion example/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ const app = express();
const router = express.Router();
const port = 3001;
const API_KEY = 'example-api-key';
const URL = 'http://localhost:3000';

app.use(cors());

const uploadJet = new UploadJet({ apiKey: API_KEY });
const uploadJet = new UploadJet({ apiKey: API_KEY, url: URL });
const uploadRouteConfig: UploadOptions = {
fileType: 'image',
maxFileSize: '1MB',
Expand Down
10 changes: 5 additions & 5 deletions packages/express/apiProposal.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ Once the package is installed, you can import the library by using import or
require approach:

```javascript
import UploadJet from 'upload-jet';
import UploadJet from '@upload-jet/express';

// or

const UploadJet = require('upload-jet');
const UploadJet = require('@upload-jet/express');
```

## Usage

To use the uploader it is required to provide an api key when creating
a new instance of Upload Jet class.
To use the uploader it is required to provide an api key and specify the URL of the Upload-Jet server when creating a new instance of the Upload Jet class.

```javascript
const uploadJet = new UploadJet({ apiKey: API_KEY });
const URL = 'SERVER_URL';
const uploadJet = new UploadJet({ apiKey: API_KEY, url: URL });
```

### Registering a new route
Expand Down
3 changes: 2 additions & 1 deletion packages/express/src/schema/upload-jet-config.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { z } from 'zod';

export const uploadJetConfigSchema = z
.object({
apiKey: z.string()
apiKey: z.string(),
url: z.string()
})
.strict();

Expand Down
5 changes: 3 additions & 2 deletions packages/express/src/upload-jet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ import { createUploadPolicyBodySchema } from './schema/create-upload-policy-body
import * as bytes from 'bytes';
import { ZodError } from 'zod';

const API_URL = 'http://localhost:3000';
const BAD_REQUEST_CODE = 400;
const INTERNAL_SERVER_ERROR_CODE = 500;
const DEFAULT_SERVER_ERROR_MESSAGE = 'Something went wrong.';

export class UploadJet {
#apiKey: string;
#url: string;

constructor(config: UploadJetConfig) {
const data = uploadJetConfigSchema.parse(config);
this.#apiKey = data.apiKey;
this.#url = data.url;
}

createUploadRoute(options: UploadOptions) {
Expand Down Expand Up @@ -96,7 +97,7 @@ export class UploadJet {
{}
);

const url = new URL('upload-policy', API_URL);
const url = new URL('upload-policy', this.#url);
const headers = { Authorization: `Bearer ${this.#apiKey}` };
return axios
.post(url.href, policyRules, { headers })
Expand Down
2 changes: 1 addition & 1 deletion upload-jet/src/application/application.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class Application extends BaseEntity {
@Property({ persist: false })
get keyHint() {
const apiKeys = this.apiKeys.getItems();
return apiKeys.find(apiKey => !apiKey.deletedAt).keyHint;
return apiKeys.find(apiKey => !apiKey.deletedAt)?.keyHint;
}

constructor(name: string, userId: number) {
Expand Down