Compare commits
5 Commits
@nhost/str
...
@nhost/rea
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1a4a061284 | ||
|
|
78555c7e85 | ||
|
|
01ded8ffff | ||
|
|
3c7cf92edf | ||
|
|
bb4301fd34 |
@@ -1,5 +1,12 @@
|
||||
# @nhost/dashboard
|
||||
|
||||
## 0.11.19
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- @nhost/react-apollo@5.0.6
|
||||
- @nhost/nextjs@1.13.11
|
||||
|
||||
## 0.11.18
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@nhost/dashboard",
|
||||
"version": "0.11.18",
|
||||
"version": "0.11.19",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"preinstall": "npx only-allow pnpm",
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
# @nhost/apollo
|
||||
|
||||
## 5.0.5
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [3c7cf92e]
|
||||
- @nhost/nhost-js@2.0.5
|
||||
|
||||
## 5.0.4
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@nhost/apollo",
|
||||
"version": "5.0.4",
|
||||
"version": "5.0.5",
|
||||
"description": "Nhost Apollo Client library",
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
# @nhost/react-apollo
|
||||
|
||||
## 5.0.6
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- @nhost/apollo@5.0.5
|
||||
- @nhost/react@2.0.5
|
||||
|
||||
## 5.0.5
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@nhost/react-apollo",
|
||||
"version": "5.0.5",
|
||||
"version": "5.0.6",
|
||||
"description": "Nhost React Apollo client",
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
# @nhost/react-urql
|
||||
|
||||
## 2.0.5
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- @nhost/react@2.0.5
|
||||
|
||||
## 2.0.4
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@nhost/react-urql",
|
||||
"version": "2.0.4",
|
||||
"version": "2.0.5",
|
||||
"description": "Nhost React URQL client",
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
# @nhost/nextjs
|
||||
|
||||
## 1.13.11
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- @nhost/react@2.0.5
|
||||
|
||||
## 1.13.10
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@nhost/nextjs",
|
||||
"version": "1.13.10",
|
||||
"version": "1.13.11",
|
||||
"description": "Nhost NextJS library",
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
# @nhost/nhost-js
|
||||
|
||||
## 2.0.5
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 3c7cf92e: fixing generating the correct URL for function calls
|
||||
|
||||
## 2.0.4
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@nhost/nhost-js",
|
||||
"version": "2.0.4",
|
||||
"version": "2.0.5",
|
||||
"description": "Nhost JavaScript SDK",
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
@@ -58,14 +58,13 @@
|
||||
"verify:fix": "run-p prettier:fix lint:fix"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nhost/graphql-js": "workspace:*",
|
||||
"@nhost/hasura-auth-js": "workspace:*",
|
||||
"@nhost/hasura-storage-js": "workspace:*",
|
||||
"@nhost/graphql-js": "workspace:*",
|
||||
"cross-fetch": "^3.1.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"graphql": "16.6.0",
|
||||
"start-server-and-test": "^1.15.2"
|
||||
"graphql": "16.6.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import fetch from 'cross-fetch'
|
||||
import { urlFromSubdomain } from '../../utils/helpers'
|
||||
import { buildUrl, urlFromSubdomain } from '../../utils/helpers'
|
||||
import { NhostClientConstructorParams } from '../../utils/types'
|
||||
import {
|
||||
NhostFunctionCallConfig,
|
||||
@@ -65,11 +65,10 @@ export class NhostFunctionsClient {
|
||||
...config?.headers
|
||||
}
|
||||
|
||||
const backendUrl = this.url
|
||||
const functionUrl = url.startsWith('/') ? url : `/${url}`
|
||||
const fullUrl = buildUrl(this.url, url)
|
||||
|
||||
try {
|
||||
const result = await fetch(`${backendUrl}/${functionUrl}`, {
|
||||
const result = await fetch(fullUrl, {
|
||||
body: JSON.stringify(body),
|
||||
headers,
|
||||
method: 'POST'
|
||||
@@ -123,7 +122,7 @@ export class NhostFunctionsClient {
|
||||
this.accessToken = accessToken
|
||||
}
|
||||
|
||||
private generateAccessTokenHeaders(): NhostFunctionCallConfig['headers'] {
|
||||
generateAccessTokenHeaders(): NhostFunctionCallConfig['headers'] {
|
||||
if (this.adminSecret) {
|
||||
return {
|
||||
'x-hasura-admin-secret': this.adminSecret
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { NhostClientConstructorParams } from './types'
|
||||
|
||||
// a port can be a number or a placeholder string with leading and trailing double underscores, f.e. "8080" or "__PLACEHOLDER_NAME__"
|
||||
const LOCALHOST_REGEX = /^((?<protocol>http[s]?):\/\/)?(?<host>localhost)(:(?<port>(\d+|__\w+__)))?$/
|
||||
export const LOCALHOST_REGEX =
|
||||
/^((?<protocol>http[s]?):\/\/)?(?<host>localhost)(:(?<port>(\d+|__\w+__)))?$/
|
||||
|
||||
/**
|
||||
* `backendUrl` should now be used only when self-hosting
|
||||
@@ -73,3 +74,16 @@ function getValueFromEnv(service: string) {
|
||||
|
||||
return process.env[`NHOST_${service.toUpperCase()}_URL`]
|
||||
}
|
||||
|
||||
/**
|
||||
* Combines a base URL and a path into a single URL string.
|
||||
*
|
||||
* @param baseUrl - The base URL to use.
|
||||
* @param path - The path to append to the base URL.
|
||||
* @returns The combined URL string.
|
||||
*/
|
||||
export function buildUrl(baseUrl: string, path: string) {
|
||||
const hasLeadingSlash = path.startsWith('/')
|
||||
const urlPath = hasLeadingSlash ? path : `/${path}`
|
||||
return baseUrl + urlPath
|
||||
}
|
||||
|
||||
79
packages/nhost-js/tests/functions.test.ts
Normal file
79
packages/nhost-js/tests/functions.test.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import { describe, it, expect, afterEach, beforeEach } from 'vitest'
|
||||
import { urlFromSubdomain } from '../src/utils/helpers'
|
||||
import { createFunctionsClient, NhostFunctionsClient } from '../src/clients/functions'
|
||||
|
||||
describe('createFunctionsClient', () => {
|
||||
it('should throw an error if neither subdomain nor functionsUrl are provided', () => {
|
||||
expect(() => {
|
||||
createFunctionsClient({})
|
||||
}).toThrow()
|
||||
})
|
||||
|
||||
it('should throw an error if a non localhost subdomain is used without a region', () => {
|
||||
const subdomain = 'test-subdomain'
|
||||
expect(() => {
|
||||
createFunctionsClient({ subdomain })
|
||||
}).toThrow()
|
||||
})
|
||||
|
||||
it('should create a client with localhost as a subdomain without a region subdomain', () => {
|
||||
const subdomain = 'localhost'
|
||||
const client = createFunctionsClient({ subdomain })
|
||||
|
||||
expect(client).toBeInstanceOf(NhostFunctionsClient)
|
||||
expect(client.url).toEqual(urlFromSubdomain({ subdomain }, 'functions'))
|
||||
})
|
||||
|
||||
it('should create a client with non localhost subdomain and any region', () => {
|
||||
const subdomain = 'localhost'
|
||||
const region = 'eu-central-1'
|
||||
const client = createFunctionsClient({ subdomain, region })
|
||||
|
||||
expect(client).toBeInstanceOf(NhostFunctionsClient)
|
||||
expect(client.url).toEqual(urlFromSubdomain({ subdomain, region }, 'functions'))
|
||||
})
|
||||
|
||||
it('should create a client with functionsUrl', () => {
|
||||
const functionsUrl = 'http://test-functions-url'
|
||||
const client = createFunctionsClient({ functionsUrl })
|
||||
|
||||
expect(client).toBeInstanceOf(NhostFunctionsClient)
|
||||
expect(client.url).toEqual(functionsUrl)
|
||||
})
|
||||
})
|
||||
|
||||
describe('NhostFunctionsClient', () => {
|
||||
let client: NhostFunctionsClient
|
||||
|
||||
beforeEach(() => {
|
||||
client = new NhostFunctionsClient({ url: 'http://test-url' })
|
||||
})
|
||||
|
||||
it('should set the access token', () => {
|
||||
const accessToken = 'test-access-token'
|
||||
|
||||
client.setAccessToken(accessToken)
|
||||
|
||||
expect(client.generateAccessTokenHeaders()).toEqual({
|
||||
Authorization: `Bearer ${accessToken}`
|
||||
})
|
||||
})
|
||||
|
||||
it('should clear the access token', () => {
|
||||
const accessToken = 'test-access-token'
|
||||
|
||||
client.setAccessToken(accessToken)
|
||||
client.setAccessToken(undefined)
|
||||
|
||||
expect(client.generateAccessTokenHeaders()).toEqual({})
|
||||
})
|
||||
|
||||
it('should generate headers with admin secret', () => {
|
||||
const adminSecret = 'test-admin-secret'
|
||||
const clientWithAdminSecret = new NhostFunctionsClient({ url: 'http://test-url', adminSecret })
|
||||
|
||||
expect(clientWithAdminSecret.generateAccessTokenHeaders()).toEqual({
|
||||
'x-hasura-admin-secret': adminSecret
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { urlFromSubdomain } from '../src/utils/helpers'
|
||||
import { buildUrl, LOCALHOST_REGEX, urlFromSubdomain } from '../src/utils/helpers'
|
||||
|
||||
describe('urlFromParams', () => {
|
||||
describe('when using backendUrl', () => {
|
||||
@@ -81,3 +81,69 @@ describe('urlFromParams', () => {
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('buildUrl', () => {
|
||||
it('should combine base URL and path', () => {
|
||||
const baseUrl = 'https://example.com'
|
||||
const path = '/api/users'
|
||||
expect(buildUrl(baseUrl, path)).toBe('https://example.com/api/users')
|
||||
})
|
||||
|
||||
it('should add missing leading slash to path', () => {
|
||||
const baseUrl = 'https://example.com'
|
||||
const path = 'api/users'
|
||||
expect(buildUrl(baseUrl, path)).toBe('https://example.com/api/users')
|
||||
})
|
||||
|
||||
it('should handle empty base URL', () => {
|
||||
const baseUrl = ''
|
||||
const path = '/api/users'
|
||||
expect(buildUrl(baseUrl, path)).toBe('/api/users')
|
||||
})
|
||||
|
||||
it('should handle empty path', () => {
|
||||
const baseUrl = 'https://example.com'
|
||||
const path = ''
|
||||
expect(buildUrl(baseUrl, path)).toBe('https://example.com/')
|
||||
})
|
||||
|
||||
it('should handle missing parameters', () => {
|
||||
expect(() => buildUrl()).toThrow()
|
||||
expect(() => buildUrl('https://example.com')).toThrow()
|
||||
})
|
||||
})
|
||||
|
||||
describe('LOCALHOST_REGEX', () => {
|
||||
it('should match localhost without protocol or port', () => {
|
||||
const input = 'localhost'
|
||||
const match = input.match(LOCALHOST_REGEX)
|
||||
expect(match?.groups).toEqual({ host: 'localhost', protocol: undefined, port: undefined })
|
||||
})
|
||||
|
||||
it('should match localhost with http protocol', () => {
|
||||
const input = 'http://localhost'
|
||||
const match = input.match(LOCALHOST_REGEX)
|
||||
expect(match?.groups).toEqual({ host: 'localhost', protocol: 'http', port: undefined })
|
||||
})
|
||||
|
||||
it('should match localhost with https protocol and port', () => {
|
||||
const input = 'https://localhost:8443'
|
||||
const match = input.match(LOCALHOST_REGEX)
|
||||
expect(match?.groups).toEqual({ host: 'localhost', protocol: 'https', port: '8443' })
|
||||
})
|
||||
|
||||
it('should match localhost with named port placeholder', () => {
|
||||
const input = 'http://localhost:__PORT_NAME__'
|
||||
const match = input.match(LOCALHOST_REGEX)
|
||||
expect(match?.groups).toEqual({ host: 'localhost', protocol: 'http', port: '__PORT_NAME__' })
|
||||
})
|
||||
|
||||
it('should not match other URLs', () => {
|
||||
const input1 = 'https://www.example.com'
|
||||
const input2 = 'http://127.0.0.1:3000'
|
||||
const match1 = input1.match(LOCALHOST_REGEX)
|
||||
const match2 = input2.match(LOCALHOST_REGEX)
|
||||
expect(match1).toBeNull()
|
||||
expect(match2).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
# @nhost/react
|
||||
|
||||
## 2.0.5
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [3c7cf92e]
|
||||
- @nhost/nhost-js@2.0.5
|
||||
|
||||
## 2.0.4
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@nhost/react",
|
||||
"version": "2.0.4",
|
||||
"version": "2.0.5",
|
||||
"description": "Nhost React library",
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
# @nhost/vue
|
||||
|
||||
## 1.13.11
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [3c7cf92e]
|
||||
- @nhost/nhost-js@2.0.5
|
||||
|
||||
## 1.13.10
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@nhost/vue",
|
||||
"version": "1.13.10",
|
||||
"version": "1.13.11",
|
||||
"description": "Nhost Vue library",
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
|
||||
4
pnpm-lock.yaml
generated
4
pnpm-lock.yaml
generated
@@ -1027,7 +1027,6 @@ importers:
|
||||
'@nhost/hasura-storage-js': workspace:*
|
||||
cross-fetch: ^3.1.5
|
||||
graphql: 16.6.0
|
||||
start-server-and-test: ^1.15.2
|
||||
dependencies:
|
||||
'@nhost/graphql-js': link:../graphql-js
|
||||
'@nhost/hasura-auth-js': link:../hasura-auth-js
|
||||
@@ -1035,7 +1034,6 @@ importers:
|
||||
cross-fetch: 3.1.5
|
||||
devDependencies:
|
||||
graphql: 16.6.0
|
||||
start-server-and-test: 1.15.2
|
||||
|
||||
packages/react:
|
||||
specifiers:
|
||||
@@ -12817,7 +12815,7 @@ packages:
|
||||
'@babel/plugin-transform-react-jsx-source': 7.19.6_@babel+core@7.20.5
|
||||
magic-string: 0.27.0
|
||||
react-refresh: 0.14.0
|
||||
vite: 4.0.2_@types+node@18.11.17
|
||||
vite: 4.0.2_@types+node@16.18.11
|
||||
transitivePeerDependencies:
|
||||
- supports-color
|
||||
dev: true
|
||||
|
||||
Reference in New Issue
Block a user