Giter VIP home page Giter VIP logo

Comments (9)

isBatak avatar isBatak commented on July 24, 2024 1

I would like it to work with
NEXT_PUBLIC_API_ENDPOINT=http://localhost:3000/api/v1
and
NEXT_PUBLIC_API_ENDPOINT=http://localhost:3000/api/v1/

from datx.

isBatak avatar isBatak commented on July 24, 2024

Just a thought.
Is it necessary to use url-join lib when we now have native support with new URL https://developer.mozilla.org/en-US/docs/Web/API/URL/URL

const u = new URL('/todos', 'http://example.com/api/v1/');
u.toString()
// 'http://example.com/todos'

const u = new URL('/todos', 'http://example.com/api/v1')
u.toString()
// 'http://example.com/todos'

from datx.

isBatak avatar isBatak commented on July 24, 2024

@Jokasxy this line is the culprit https://github.com/infinum/datx/blob/master/packages/datx-jsonapi/src/helpers/url.ts#L158
I think if we wrapp it in new URL it will fix the problem.
You can contribute if you have time.

@DarkoKukovec what do you think, am I right?

from datx.

Jokasxy avatar Jokasxy commented on July 24, 2024

@isBatak Like you have got there, you loose this "/api/v1" part like that so you would probably need to split the host from the rest in second param and append it before to the first param to use new URL

const path = 'tournament/1';
const apiURL = new URL(process.env.NEXT_PUBLIC_API_ENDPOINT as string);
const url = new URL(`${apiURL.href}${path.length <= 1 || path.startsWith('/') ? path : `/${path}`}`);
console.log(url.href);
// http://localhost:3000/api/v1/tournament/1
// same with /tournament/1
// path = '' gives http://localhost:3000/api/v1
// path = '/' gives http://localhost:3000/api/v1/

from datx.

DarkoKukovec avatar DarkoKukovec commented on July 24, 2024

Actually, it's this line: https://github.com/infinum/datx/blob/master/packages/datx-jsonapi/src/helpers/url.ts#L90

Is the issue that you have to add / here? It seems to me that it's fine to be explicit here, because otherwise we're assuming that / is always at the end, which might not be true?

from datx.

Jokasxy avatar Jokasxy commented on July 24, 2024

With this ${config.baseUrl}${url}, slash character needs to be added either at the end of {baseUrl} or at the start of the {url} e.g. public static readonly endpoint = '/tournaments';.

My idea adding a util function which will recognize how the url is formatted which could prevent zero and double slashes so we have a valid url anyhow.

from datx.

isBatak avatar isBatak commented on July 24, 2024

I agree with @Jokasxy double slashes should not happen.
I had this issue before and I scratched my head for some time until I figured out what was the problem.

from datx.

DarkoKukovec avatar DarkoKukovec commented on July 24, 2024

I guessd we could try to prevent double slashes, but I'm njot convinced we should add slashes if they don't exist.

from datx.

isBatak avatar isBatak commented on July 24, 2024

Final conclusion was to implement predictable url building that avoids double shals issue.
To opt-out from this decision endpoint should accept transformer function that receives baseUrl and returns newly constructed endpoint.

config.baseUrl = 'http://example.com/api';

class Example extends jsonapi(Model) {
  public static readonly endpoint = '_tournaments';
}
// endpoint: http://example.com/api/_tournaments

class Example extends jsonapi(Model) {
  public static readonly endpoint = (baseUrl: string) => `${baseUrl}_tournaments`;
}

// endpoint: http://example.com/api_tournaments

from datx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.