shakacode/react_on_rails

View on GitHub
node_package/src/Authenticity.ts

Summary

Maintainability
A
0 mins
Test Coverage
import type { AuthenticityHeaders } from './types/index';

export default {
  authenticityToken(): string | null {
    const token = document.querySelector('meta[name="csrf-token"]');
    if (token && (token instanceof window.HTMLMetaElement)) {
      return token.content;
    }
    return null;
  },

  authenticityHeaders(otherHeaders: {[id: string]: string} = {}): AuthenticityHeaders {
    return Object.assign(otherHeaders, {
      'X-CSRF-Token': this.authenticityToken(),
      'X-Requested-With': 'XMLHttpRequest',
    });
  },
};